gpt4 book ai didi

swift - 检测何时从单元格本身中选择自定义单元格?

转载 作者:搜寻专家 更新时间:2023-11-01 05:52:44 25 4
gpt4 key购买 nike

我已经创建了一个自定义的 UITableViewCell(连同一个 XIB,用于 Storyboard设计器中的布局)。我了解父 TableView 如何通过触发 didSelectRowAtIndexPath 通知单元格选择,但我似乎无法弄清楚如何在单元格本身内捕获单元格的选择。有人可以在这里指出我正确的方向吗?我正在使用 XCode 8 和 Swift 2。谢谢!

这是我的简单自定义单元格类,带有 stub 函数以在选择单元格时进行处理:

class MyCustomCell: UITableViewCell {
func didSelect(indexPath: NSIndexPath ) {
// perform some actions here
}
}

最佳答案

您可以做的是在 UITableView 上监听 didSelectRowAtIndexPath,然后调用单元格中的函数。这是一个例子:

class MyCustomCell: UITableViewCell {
func didSelect(indexPath: NSIndexPath) {
// perform some actions here
}
}

然后,在您的 didSelectRowAtIndexPath 中:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? MyCustomCell {
cell.didSelect(indexPath: indexPath)
}
}

关于swift - 检测何时从单元格本身中选择自定义单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41581820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com