【发布时间】:2015-09-20 05:42:42
【问题描述】:
我有一个验证函数循环遍历我的表格视图,问题是它在某些时候返回 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 行返回一个 nil 单元格......我也可以在 UI 中看到我的 3 单元格。
有人知道我做错了什么吗?
我的函数在一些 tableView.reloadData() 和 viewDidLoad 之后被调用,是否有可能在我的函数执行事件之前 tableview 没有完成重新加载,尽管我没有在 dispatch_async 中调用它??
希望有答案。 提前致谢
--------------- 答案 ------- -----
补充说明:
cellForRowAtIndexPath 仅返回可见单元格,应在数据模型中进行验证。当细胞构造在
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
它应该根据验证状态改变自己。
【问题讨论】:
标签: swift tableview nsindexpath