首页 文章

在reloadData之后没有调用cellForRowAtIndexPath

提问于
浏览
0

我有一个表视图控制器,需要通过委托调用进行更新 . 我已经设置了数据源和委托,并且在tableview的初始加载时,所有工作都按预期工作 . 我有一个在数据源更新后调用的委托方法 . 委托调用表视图控制器类中的刷新方法,该方法调用.reloadData()

调用reloadData时,将调用numberOfRowsInSection并准确返回行数,但不会调用cellForRowAtIndexPath .

在这种特殊情况下,numberOfRowsInSection返回2,因此应该调用cellForRowAtIndexPath两次,但它被称为零次 .

在初始加载时一切都很好 . 只有当调用reloadData时,才会忽略cellForRowAtIndexPath . 我在Obj-C中多次做过同样的事情,没有任何奇怪之处 . 在Swift中是否存在任何已知问题?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier(LayerMenuCell.reuseId) as! LayerMenuCell
    // ....
    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print(layerEntries?.count)
    return (layerEntries?.count)!
}

func refresh() {
    self.layersTableView.reloadData()
}

谢谢!

3 回答

  • 0

    尝试设置 UITableViewdelegatedataSource

    myTable.delegate = self
    myTable.dataSource = self
    
  • -2

    如文档中所述:

    功能正常的表视图需要三个表视图数据源方法 .

    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    

    确保您实现上述三个委托方法,并返回除nil或0以外的一些值 .

    在两种情况下,单元格高度可能为0 /表高度为0的情况下,不会调用行方法的单元格 .

    还要确保正确设置委托并在主线程上调用reloadData方法 . 更多关于here

  • 4

    Tableview无法按预期工作时需要检查的事项:
    1.通过故事板或代码设置委托和数据源 .

    self.tableView.delegate = self
    self.tableView.dataSource = self
    

    2.检查storyboard中的tableView是否连接到tableView outlet .
    3.check numberOfRowsInSection和numberOfSectionsInTableView返回正确的值 .
    4.检查方法是否写得正确 .
    5.在UIViewController之后添加委托和数据源 .

    <UITableViewDelegate , UITableViewDataSource>
    

    如果你遗漏任何东西,这将对你有所帮助 .

相关问题