Ios 从UITapGestureRecognitor获取UITableViewCell

Ios 从UITapGestureRecognitor获取UITableViewCell,ios,uitableview,swift,uitapgesturerecognizer,Ios,Uitableview,Swift,Uitapgesturerecognizer,我有一个包含多个部分的UITableView,在我的CellForRowatineXpath方法中,我向单元格的imageView添加了一个UITapGestureRecognitor(每个单元格都有一个小图像)。我已通过以下方式成功访问该图像(以便更改): var imageView : UIImageView! = sender.view! as UIImageView 然而,我现在需要做的是访问单元格的数据,我相信这意味着我需要能够访问单元格以获取节和行号。有人能建议怎么做吗?我的想法是

我有一个包含多个部分的UITableView,在我的CellForRowatineXpath方法中,我向单元格的imageView添加了一个UITapGestureRecognitor(每个单元格都有一个小图像)。我已通过以下方式成功访问该图像(以便更改):

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

然而,我现在需要做的是访问单元格的数据,我相信这意味着我需要能够访问单元格以获取节和行号。有人能建议怎么做吗?我的想法是,如果任务完成,我会将图像更改为未选中复选框,但在我的数据中,我需要实际将该任务标记为完成。

在处理点击手势识别器的函数中,使用
tableView
indexPathForRowAtPoint
功能获取触摸事件的可选索引路径,如下所示:

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
来获取单元格或其任何内容,因为您现在有了被点击单元格的索引路径。

您可以获取被点击图像的位置,以便找到indexPath,并从中找到具有该图像的单元格:

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

您缺少可以在此处使用委托设计模式的功能

创建一个协议,告知代理复选框(imageView)中的状态更改:

假设您有一个
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)
    }

}
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

    }

}
UITableViewController
子类中:

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)
    }

}
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

    }

}

希望这有帮助

获取特定单元格上tableView的单元格位置。调用一个函数,该函数保存单元格对象(如UIImage等)的手势

let location = gesture.location(in: tableView)
   let indexPath = tableView.indexPathForRow(at: location)
   Print(indexPath.row)
   Print(indexPath.section)

嗨,这几乎成功了!但是,位置必须不正确,因为它在错误的单元格上执行操作。有什么想法吗?嗨,这几乎奏效了!但是,位置必须不正确,因为它在错误的单元格上执行操作。对此有何想法?应该是:var cell=self.tableView.cellforrowatinexpath(indexPath)
UITableViewCell
没有
viewDidLoad
方法使用awakeFromNib而不是viewDidLoad