ios - UITableView didSelectRowAt 无法获取我的单元格

标签 ios swift uitableview

我有一个问题。 我有一个 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/

相关文章:

iOS 应用程序——在某些设备上无法通过蜂窝网络访问我们的域

ios - 在 Swift(iOS)和 setValue forKey 中子类化 NSObject 的子类

ios - 快速更改自动更正工具栏的背景颜色

ios - 网址缓存(iOS)。 storeCachedResponse 异步工作。如何 catch 完成?

ios - NSUserDefaults 线程在 IOS 上的扩展之间共享数据是否安全?

ios - 通过 FaSTLane 监控 Git 标签以自动生成构建

swift - 在 swift 中使用内部结构时引用包含类的属性

iphone - 如何改变uitableviewcell的宽度

ios - 创建子类并覆盖 loadView() 时 UITableViewController.tableView=nil

ios - 如何在 View 之间传递数据。我应该什么时候使用什么?