我有一个带有按钮的表视图,当其中一个按钮被点击时,我想使用indexpath.row。这是我目前所拥有的,但始终是0。var point = Int()func buttonPressed(sender: AnyObject) {
let pointInTable: CGPoint = sender.convertPoint(sender.bounds.origin, toView: self.tableView)
let cellIndexPath = self.tableView.indexPathForRowAtPoint(pointInTable)
println(cellIndexPath)
point = cellIndexPath!.row
println(point)}SWIFT:当单元格中的按钮被点击时,如何获得indexpath.row?
3 回答
MMTTMM
TA贡献1869条经验 获得超4个赞
contentView
guard let cell = sender.superview?.superview as? YourCellClassHere else { return // or fatalError() or whatever}let indexPath = itemTable.indexPath(for: cell)
UITableViewCell
class MyCell: UITableViewCell { var button: UIButton! var buttonAction: ((Any) -> Void)? @objc func buttonPressed(sender: Any) { self.buttonAction?(sender) }}
cellForRowAtIndexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCell cell.buttonAction = { sender in // Do whatever you want from your button here. } // OR cell.buttonAction = buttonPressed(closure: buttonAction, indexPath: indexPath) // <- Method on the view controller to handle button presses.}
indexPath
- 3 回答
- 0 关注
- 1433 浏览
添加回答
举报
0/150
提交
取消