ios - 如何从自定义 UITableview 单元格获取 tableView 实例?

标签 ios swift uitableview

我有一个带有自定义 UITableViewCell 的 tableView,在该单元类中我想访问持有它的 tableView。为此,我使用 cell.superView ,它返回为零。但当我不使用任何自定义 UITableViewCell 时,同样工作正常。

这是我的代码,

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TestTableViewCell
    cell?.textLabel?.text = "Test"
    return cell!
}

class TestTableViewCell: UITableViewCell {

override func awakeFromNib() {
    super.awakeFromNib()
}

func loadDetails() {
    let tableView = self.superview //which is returning nil here
}}

最佳答案

UITableViewDelegate 上,当点击单元格时,您可以知道哪个 UITableView 持有它。

触发的方法是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // tableView is the UITableView holding the cell.

}

关于ios - 如何从自定义 UITableview 单元格获取 tableView 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447538/

相关文章:

iphone - UIKeyboardDidHideNotification 错误应用程序崩溃

ios - iOS 3.x 上的 setShadowOffset 崩溃

ios - __block 没有 block 的存储类型?

swift - 是否可以在端口 80 上使用 Realm Mobile Platform 同步服务器?

ios - 将不同的图钉图像与不同的位置 iOS 相关联

ios - 使自定义单元格 100% 透明

ios - Xcode 8 和 Swift 3 中 Unresolved 标识符错误

swift - 如何打开带有事件的日历 - NSURL calshow :

c# - MvxTableViewCell 上的属性文本

ios - 尝试向 iOS 中的 UITableView 动态添加新行