0

In Swift, I have created a custom cell with 3 labels and in which I have added a button or type UIButton. The button is displayed in each row of the table. When I click on the button I would like to save the indexPath of the row in which the button was pressed so I can replace the labels with different text. It is unclear to me how I could proceed. I would like to call the accessory button method (accessoryButtonTappedForRowWithIndexPath) if possible.

Thank you for any guidance.

Syed Tariq
  • 2,878
  • 3
  • 27
  • 37

2 Answers2

0

One thing you could do is in cellForRowAtIndexPath, set the tag of the UIButton for that cell equal to the indexPath.row. When the button is tapped, it can trigger

- (void) accessoryButtonTapped: (UIButton *) sender{
    int rowNum = sender.tag;
}

So now you have the rowNum of the button that was tapped. Not sure what you want to do with it though...

Josh Gafni
  • 2,831
  • 2
  • 19
  • 32
0

I think you have pretty much answered you own question but the trick is to set the button as an accessory view. For this accessory view, use a selector; e.g. didClickAccessoryView and then eventually call accessoryButtonTappedForRowWithIndexPath from there.

Here's an objective-c implementation for the same:

Using a custom image for a UITableViewCell's accessoryView and having it respond to UITableViewDelegate

Community
  • 1
  • 1
p0lAris
  • 4,750
  • 8
  • 45
  • 80
  • Thank you for your guidance. Here is what I tried: @IBAction func showSubcategoryEditor(sender: UIButton) { if let indexPath = tableView.indexPathForRowAtPoint(sender.frame) as NSIndexPath { tableView.delegate.accessoryButtonTappedForRowWithIndexPath(indexPath) } } override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) { savedRow = indexPath.row self.performSegueWithIdentifier("SubcategorySegue", sender: self) } However I get the follwoing error message: – Syed Tariq Jan 31 '15 at 15:05
  • (UITableView, numberOfRowsInSection: Int) -> Int' does not have a member named 'indexPathForRowAtPoint – Syed Tariq Jan 31 '15 at 15:09