从 UITapGestureRecognizer 获取 UITableViewCell

6

我有一个UITableView,其中包含多个部分,在我的cellForRowAtIndexPath方法中,我向单元格的imageView(每个单元格都有一个小图像)添加了一个UITapGestureRecognizer。通过使用以下代码,我成功地访问了该图像(以便更改它):

var imageView : UIImageView! = sender.view! as UIImageView

现在我需要做的是访问单元格的数据,这意味着我需要能够访问单元格以获取部分和行号。有人可以建议如何做到这一点吗?我的想法是,如果任务已完成,则将图像更改为未选中的复选框,但是在我的数据中,我实际上需要标记该任务已完成。
4个回答

21
在处理点击手势识别器的函数中,使用tableViewindexPathForRowAtPoint函数来获取触摸事件的可选索引路径,代码如下:
func handleTap(sender: UITapGestureRecognizer) {
    let touch = sender.locationInView(tableView)
    if let indexPath = tableView.indexPathForRowAtPoint(touch) {
        // Access the image or the cell at this index path
    }
}

从这里开始,您可以调用cellForRowAtIndexPath来获取被点击单元格的单元格或其任何内容,因为您现在拥有该单元格的索引路径。


嗨,这几乎有效了!但是,位置可能不正确,因为它正在对错误的单元格执行操作。您有什么想法吗? - iOSBeginner

6

您可以获取被点击图像的位置,以找到indexPath,然后从那里找到包含该图像的单元格:

var position: CGPoint =  sender.locationInView(self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(position)!    
var cell = self.tableView(tableView, cellForRowAtIndexPath: indexPath)

嗨,这几乎成功了!但是位置可能不正确,因为它正在对错误的单元格执行操作。您有什么想法吗? - iOSBeginner
1
应该是:var cell = self.tableView.cellForRowAtIndexPath(indexPath) - Josh

2

你忽略了可以在这里使用委托设计模式。

创建一个协议,告诉代理你的复选框(imageView)的状态变化:

enum CheckboxState: Int {
    case .Checked = 1
    case .Unchecked = 0
}

protocol MyTableViewCellDelegate {
    func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState)
}

假设您有一个UITableViewCell子类:

class MyTableViewCell: UITableViewCell {

    var delegate: MyTableViewCellDelegate?

    func viewDidLoad() {

        // Other setup here...

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapImage:")
        imageView.addGestureRecognizer(tapGestureRecognizer)
        imageView.userInteractionEnabled = true
    }

    func didTapImage(sender: AnyObject) {

        // Set checked/unchecked state here
        var newState: CheckboxState = .Checked // depends on whether the image shows checked or unchecked

        // You pass in self as the tableViewCell so we can access it in the delegate method
        delegate?.tableViewCell(self, didChangeCheckboxToState: newState)
    }

}

在您的 UITableViewController 子类中:

class MyTableViewController: UITableViewController, MyTableViewCellDelegate {

    // Other methods here (viewDidLoad, viewDidAppear, etc)...

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) {
        var cell = tableView.dequeueReusableCellWithIdentifier("YourIdentifier", forIndexPath: indexPath) as MyTableViewCell

        // Other cell setup here...

        // Assign this view controller as our MyTableViewCellDelegate
        cell.delegate = self

        return cell
    }

    override func tableViewCell(tableViewCell: MyTableViewCell, didChangeCheckboxToState state: CheckboxState) {

        // You can now access your table view cell here as tableViewCell

    }

}

希望这能帮到您!

1
UITableViewCell没有viewDidLoad方法。 - Rodrigo Ruiz
使用 awakeFromNib 而不是 viewDidLoad。 - djdance

0
获取tableView上特定单元格的位置。调用一个包含单元格对象手势(如UIImage等)的函数。
let location = gesture.location(in: tableView)
   let indexPath = tableView.indexPathForRow(at: location)
   Print(indexPath.row)
   Print(indexPath.section)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接