Toshi
Toshi

Reputation: 1303

How to send cell number to the function in TableViewCell

I'd like to know how to get cell number(indexPath.row) in the following tapPickView function. topView is on the UITableViewCell, and pickView is on the topView. If pickView is tapped, tapPickView is activated.

override func tableView(tableView: UITableView,
                        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(
               "QuestionAndAnswerReuseIdentifier",
               forIndexPath: indexPath) as! QuestionAndAnswerTableViewCell

    cell.topView.pickView.userInteractionEnabled = true
    var tap = UITapGestureRecognizer(target: self, action: "tapPickView")
    cell.topView.pickView.addGestureRecognizer(tap)
    return cell
}

func tapPickView() {
    answerQuestionView = AnswerQuestionView()
    answerQuestionView.questionID = Array[/*I wanna put cell number here*/]

    self.view.addSubview(answerQuestionView)
}

Upvotes: 1

Views: 112

Answers (3)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52153

First of all, you need to append : sign to your selector upon adding gesture recognizer in order for it to get the pickView as its parameter.

var tap = UITapGestureRecognizer(target: self, action: "tapPickView:")

Besides that, cells are reusable objects, so you should prevent adding same gesture again and again to the same view instance by removing previously added ones.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("QuestionAndAnswerReuseIdentifier", forIndexPath: indexPath) as! QuestionAndAnswerTableViewCell
    cell.topView.pickView.userInteractionEnabled = true
    cell.topView.pickView.tag = indexPath.row

    for recognizer in cell.topView.pickView.gestureRecognizers ?? [] {
        cell.topView.pickView.removeGestureRecognizer(recognizer)
    }

    cell.topView.pickView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "tapPickView:"))

    return cell
}

While populating the cell, you can set tag value of the pickView as indexPath.row so you can easily query that by cellForRowAtIndexPath(_:).

cell.topView.pickView.tag = indexPath.row

Assuming you already know the section of the cell you tap on. Let's say it is 0.

func tapPickView(recognizer: UIGestureRecognizer) {

    let indexPath = NSIndexPath(forRow: recognizer.view.tag, inSection: 0)

    if let cell = self.tableView.cellForRowAtIndexPath(indexPath) {
        print("You tapped on \(cell)")
    }
}

Hope this helps.

Upvotes: 1

SwiftArchitect
SwiftArchitect

Reputation: 48514

Assuming that this was not as simple as didSelectRowAtIndexPath, which I strongly recommend to first look into, passing the information to your method could look like this:

@IBAction func tapPickView:(sender: Anyobject) {
    if let cell = sender as? UITableViewCell {
        let indexPath = self.tableView.indexPathForCell(cell: cell)
        println(indexPath)
    }
}

Upvotes: 1

hris.to
hris.to

Reputation: 6363

Use didSelectRowAtIndexPath delegate method.

Upvotes: 0

Related Questions