加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – tableView.cellForRowAtIndexPath(indexPath)返回nil

发布时间:2020-12-14 02:27:36 所属栏目:百科 来源:网络整理
导读:我有一个循环通过我的表视图的验证函数,问题是它在某个时刻返回nil单元格. for var section = 0; section self.tableView.numberOfSections(); ++section { for var row = 0; row self.tableView.numberOfRowsInSection(section); ++row { var indexPath = N
我有一个循环通过我的表视图的验证函数,问题是它在某个时刻返回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
        }
    }
}

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

有谁知道我做错了什么?

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

希望得到答案.
预先感谢

—————————答案———————- –

补充说明:

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

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

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

如文档中所述,cellForRowAtIndexPath返回:

An object representing a cell of the table,or nil if the cell is not visible or indexPath is out of range.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读