tableView.cellForRowAtIndexPath(indexPath)返回nil

Inc*_*ito 6 tableview nsindexpath swift

我有一个循环通过我的表视图的验证函数,问题是它在某个时刻返回nil单元格.

for var section = 0; section < self.tableView.numberOfSections(); ++section {
    for var row = 0; row < self.tableView.numberOfRowsInSection(section); ++row {
        var indexPath = NSIndexPath(forRow: row, inSection: section)
        if section > 0 {
            let cell = tableView.cellForRowAtIndexPath(indexPath) as! MyCell
            // cell is nil when self.tableView.numberOfRowsInSection(section) return 3 for row 1 and 2
            // ... Other stuff
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定我在这里做错了什么,我尝试双重检查indexPath行和部分它们很好,numberOfRowsInSection()返回3但行1和2返回一个零单元格...我可以看到我的UI中也有3个单元格.

有谁知道我做错了什么?

我的函数在一些tableView.reloadData()和viewDidLoad之后调用,是否有可能在我的函数执行之前tableview没有完成重载事件虽然我没有在dispatch_async中调用它?

希望得到答案.预先感谢

---------------------------答案---------------------- -

补充说明:

cellForRowAtIndexPath只返回可见单元格,验证应该在数据模型中完成.当细胞构建在

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
Run Code Online (Sandbox Code Playgroud)

它应该根据验证状态自行更改.

Ant*_*nio 14

如文档中所述,cellForRowAtIndexPath返回:

表示表格单元格的对象,如果单元格不可见或indexPath超出范围,则为nil.

因此,除非您的表格完全显示,否则该方法会返回一些屏幕外行nil.

它返回nil非可见单元格的原因是因为它们不存在 - 表重用相同的单元格,以最小化内存使用 - 否则将无法管理具有大量行的表.

  • 我有一个情况,所有单元格都在屏幕上,但是 cellForRowAtIndexPath 确实返回 nil :-[[[[[ (3认同)