如何访问一个自定义按钮TableViewCell指数路径? [英] How to access index path of button in a custom TableViewCell?
问题描述
我创建了一个自定义的 TableViewCell
,目前有放置在小区的一个按钮。当按钮是pssed $ P $,在 tableviewcell.swift
文件, IBAction为FUNC
被执行。我无法弄清楚如何确定按钮在该单元的索引路径pressed。我尝试使用以下
I have created a custom TableViewCell
and currently have a button placed in the cell. When the button is pressed, In the tableviewcell.swift
file, IBAction func
gets executed. I cannot figure out how to determine the index path of the cell that the button is in that is pressed. I was trying to use the following
@IBAction func employeeAtLunch(sender: AnyObject) {
let indexPath = (self.superview as! UITableView).indexPathForCell(self)
println("indexPath?.row")
}
但我得到的点击以下错误:
类型无法施展值'UITableViewWrapperView'到'UITableView的
关于如何访问单元格的索引路径任何帮助吗?
Any help on how to access the index path of the cell?
推荐答案
您可以继承的UIButton
在你的细胞与它的行属性。
You could subclass UIButton
in your cell with a property for its row.
class MyButton: UIButton {
var row: Int?
}
然后,当你设置你的表视图,在的cellForRowAtIndexPath
方法,将行
属性:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// ...
cell.button.row = indexPath.row
// ...
}
此方式时动作火灾,就可以得到正确的行:
This way when the action fires, you can get the correct row:
@IBAction func employeeAtLunch(sender: MyButton) {
if let row = sender.row {
// access the row
}
}
这篇关于如何访问一个自定义按钮TableViewCell指数路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!