tableView.cellForRowAtIndexPath(indexPath) return 无
tableView.cellForRowAtIndexPath(indexPath) return nil
我有一个循环遍历我的 table 视图的验证函数,问题是它 return 在某些时候没有单元格。
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() return 3 但第 1 行和第 2 行 return 一个零单元格...我也可以在 UI 中看到我的 3 单元格。
有人知道我做错了什么吗?
我的函数在一些 tableView.reloadData() 之后调用,在 viewDidLoad 中,是否有可能 tableview 在我的函数执行事件之前没有完成重新加载,尽管我没有调用它在 dispatch_async ??
希望得到解答。
提前致谢
----------------------------答案---------------- ------
补充说明:
cellForRowAtIndexPath 仅 return 可见单元格,应在数据模型中进行验证。当细胞构建于
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
它应该根据验证状态自行改变。
如文档中所述,cellForRowAtIndexPath
returns:
An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.
因此,除非您的 table 已完全显示,否则该方法 returns nil
.
会显示一些屏幕外的行
不可见单元格 returns nil
的原因是因为它们不存在 - table 重复使用相同的单元格,以尽量减少内存使用 - 否则 table 具有大量行的文件将无法管理。
我也遇到过 cellForRowAtIndexPath
返回 nil 的问题,即使单元格 完全 可见。在我的例子中,我在 viewDidAppear()
中调用了调试函数(见下文),我怀疑 UITableView
还没有完全准备好,因为正在打印的部分内容不完整,没有单元格。
我是这样解决的:在 viewController 中,我放置了一个调用调试功能的按钮:
public func printCellInfo() {
for (sectionindex, section) in sections.enumerated() {
for (rowIndex, _) in section.rows.enumerated() {
let cell = tableView.cellForRow(at: IndexPath(row: rowIndex, section: sectionindex))
let cellDescription = String(describing: cell.self)
let text = """
Section (\(sectionindex)) - Row (\(rowIndex)): \n
Cell: \(cellDescription)
Height:\(String(describing: cell?.bounds.height))\n
"""
print(text)
}
}
}
Please note that I'm using my own data structure: the data source is an array of sections, each of them containing an array of rows. You'll need to
adjust accordingly.
如果我的假设是正确的,您将能够打印所有可见单元格的调试描述。请试一试,如果有效请告诉我们。
因此,要处理该错误,只需执行可选绑定:
// Do your dataSource changes above
if let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell {
// yourCode
}
如果单元格可见,则您的代码已应用或以其他方式显示,当在 cellForRowAt
方法中作为 dequeueReusableCell
进入可见部分时,将重新加载所需的单元格。
我有一个循环遍历我的 table 视图的验证函数,问题是它 return 在某些时候没有单元格。
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() return 3 但第 1 行和第 2 行 return 一个零单元格...我也可以在 UI 中看到我的 3 单元格。
有人知道我做错了什么吗?
我的函数在一些 tableView.reloadData() 之后调用,在 viewDidLoad 中,是否有可能 tableview 在我的函数执行事件之前没有完成重新加载,尽管我没有调用它在 dispatch_async ??
希望得到解答。 提前致谢
----------------------------答案---------------- ------
补充说明:
cellForRowAtIndexPath 仅 return 可见单元格,应在数据模型中进行验证。当细胞构建于
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
它应该根据验证状态自行改变。
如文档中所述,cellForRowAtIndexPath
returns:
An object representing a cell of the table, or nil if the cell is not visible or indexPath is out of range.
因此,除非您的 table 已完全显示,否则该方法 returns nil
.
不可见单元格 returns nil
的原因是因为它们不存在 - table 重复使用相同的单元格,以尽量减少内存使用 - 否则 table 具有大量行的文件将无法管理。
我也遇到过 cellForRowAtIndexPath
返回 nil 的问题,即使单元格 完全 可见。在我的例子中,我在 viewDidAppear()
中调用了调试函数(见下文),我怀疑 UITableView
还没有完全准备好,因为正在打印的部分内容不完整,没有单元格。
我是这样解决的:在 viewController 中,我放置了一个调用调试功能的按钮:
public func printCellInfo() {
for (sectionindex, section) in sections.enumerated() {
for (rowIndex, _) in section.rows.enumerated() {
let cell = tableView.cellForRow(at: IndexPath(row: rowIndex, section: sectionindex))
let cellDescription = String(describing: cell.self)
let text = """
Section (\(sectionindex)) - Row (\(rowIndex)): \n
Cell: \(cellDescription)
Height:\(String(describing: cell?.bounds.height))\n
"""
print(text)
}
}
}
Please note that I'm using my own data structure: the data source is an array of sections, each of them containing an array of rows. You'll need to adjust accordingly.
如果我的假设是正确的,您将能够打印所有可见单元格的调试描述。请试一试,如果有效请告诉我们。
因此,要处理该错误,只需执行可选绑定:
// Do your dataSource changes above
if let cell = tableView.cellForRow(at: indexPath) as? MyTableViewCell {
// yourCode
}
如果单元格可见,则您的代码已应用或以其他方式显示,当在 cellForRowAt
方法中作为 dequeueReusableCell
进入可见部分时,将重新加载所需的单元格。