我有一个问题。
我有一个 UITableView 和四个自定义 UITableViewCell。
当我滚动到底部时,我也无法在 didSelectRowAt
函数中获取我的自定义 UITableViewCell。
请给我一些建议。
谢谢。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell: ChartTableViewCell = customTableview.cellForRow(at: IndexPath(row: 0, section: 0)) as? ChartTableViewCell {
printBBLog("here.") //just When I scrolled to bottom, this don't print.
let colors = AssetViewController.Color.getAllColors()
cell.barChartView?.tapIndexWithColor(index: indexPath.row-2, color: colors[indexPath.row-2])
}
}
最佳答案
当请求的单元格当前在屏幕上不可见时,
cellForRow(at:)
将返回 nil
。当您的 TableView 滚动到底部时,第 0 行很可能不可见。
但是,您的 if
语句正在发挥作用;如果 cellForRow(at:)
返回 nil
,则您没有要更新的单元格,因此您无需在该函数中执行任何操作。
您应该在 cellForRow(at:)
数据源方法中设置第 0 行单元格下次出队时的外观。
而且,正如@Bappaditya 所指出的,您可能会因 indexPath.row-2
关于ios - UITableView didSelectRowAt 无法获取我的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952844/