检测何时从单元格本身中选择自定义单元格? [英] Detect when a custom cell is selected from within the cell itself?
问题描述
我创建了一个自定义的 UITableViewCell
(以及用于故事板设计器中的布局的 XIB).我了解父 tableview 如何通过触发 didSelectRowAtIndexPath
通知单元格选择,但我似乎无法弄清楚如何在单元格本身内捕获单元格的选择.有人可以在这里指出我正确的方向吗?我使用的是 XCode 8 和 Swift 2.谢谢!
I have created a custom UITableViewCell
(along with an XIB for layout within the storyboard designer). I understand how the parent tableview notifies on cell selection by triggering didSelectRowAtIndexPath
, but I can't seem to figure out how to catch the selection of a cell within the cell itself. Can someone point me in the right direction here? Im using XCode 8 and Swift 2. Thanks!
这是我的简单自定义单元格类,带有在单元格被选中时处理的存根函数:
Here is my simple custom cell class with the stubbed function to handle when the cell is selected:
class MyCustomCell: UITableViewCell {
func didSelect(indexPath: NSIndexPath ) {
// perform some actions here
}
}
推荐答案
你可以做的是在 UITableView 上监听 didSelectRowAtIndexPath
,然后在单元格中调用一个函数.举个例子:
What you could do is listen for didSelectRowAtIndexPath
on the UITableView, and then call a function in the cell. Here's an example:
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)
}
}
这篇关于检测何时从单元格本身中选择自定义单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!