我有一个循环通过我的表视图的验证函数,问题是它在某个时刻返回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的原因是因为它们不存在 – 表重用相同的单元格以最小化内存使用 – 否则将无法管理具有大量行的表.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。