0

My question: I wanna perform segue to two different destinations in a table view cell. One is directing destination "A" by clicking the cell which handled by didSelectRowAt, Another one is directing to destination "B" by clicking the button inside the cell, for example, the labelName.

For destiation "B", I have try to use (btn.addTarget), but it cannot add parameters, so the destinatin view controller do not know the which cell was being clicked.

Any suggestion?

Patrick
  • 1
  • 2
  • wanna try closures check this : https://stackoverflow.com/questions/51239746/swift-connect-delegate-to-custom-xib-cell/51242496#51242496 – Amit Jul 30 '18 at 05:41

3 Answers3

0

you can use button.tag and assign the tag of every button with your indexpath.row

button.tag = indexPath.row
button.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)

func btnClick(sender:UIButton) {
    let buttonRow = sender.tag
}

for swift 4 :

  button.addTarget(self, action: #selector(händlerButtonTapped), for: .touchUpInside)

    @objc func händlerButtonTapped(sender:UIButton) {
       let buttonRow = sender.tag
    }
Osman
  • 1,496
  • 18
  • 22
  • thanks, I can get the indexPath.row, but how can I get the information inside the cell, like the labelName inside one of the tableViewCell? I try to do something as follow but turns out it cannot retrieve the data. @objc func händlerButtonTapped(sender:UIButton) { let buttonRow = sender.tag let info:infoModel info = infoList[buttonRow] let name = info.labelName print("Name: \(name)") } printedMessage:(Name: Prototype.infoModel) it only showed the model name but not the value, is it a must to use didSelectRowAt to get the data inside the cell? – Patrick Jul 30 '18 at 06:09
  • no it depends to your data structure, update your answer and add the function cellForRowAt, i want to see how you presenting your label ( func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell ) – Osman Jul 30 '18 at 10:19
0

You can Get the indexPath of your selected TableviewCell in your btnClick function by using this.

let index = self.tableview.indexPathForSelectedRow

Now by doing this you can solve this problem you mentioned.

the destinatin view controller do not know the which cell was being clicked

Muhammad Shauket
  • 2,643
  • 19
  • 40
0

Here is my cellForRowAt:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! contentTableViewCell

    let content: contentModel
    content = contentList[indexPath.row]
    contentViewController().getUserProfilePic(userID:content.userID!)
    cell.detailLabelName.text = content.name
    cell.detailLabelTime.text = content.time
    cell.detailTextViewContent.text = content.content

    if UserDefaults.standard.data(forKey:content.userID!) != nil {
        cell.detailImageSelfie.image = UIImage(data:UserDefaults.standard.data(forKey:content.userID!)!)
    } else {
        cell.detailImageSelfie.image = #imageLiteral(resourceName: "defaultIcon")
    }
    return cell
}
Patrick
  • 1
  • 2