2

Maybe the question is not clearly enough.

I mean I have a TableView,and it has a Prototype Cell and this Cell has a button.

I set a segue name "ForMore" which is combine 2 ViewControllers

But I reuse this Cell many times.

And this is result:

When I click everyone of the button in all cells.It will jump to another ViewController.

But I need to know which button I have clicked because that ViewController need initialize according to which cell I clicked.

So, how can I know which cell I clicked when I reuse a cell many times.

Hashira
  • 139
  • 1
  • 9
  • Is it a custom button? – iRiziya Jul 27 '15 at 08:40
  • @FrequencyMatched No,It is a normal button.But it is tied with a UITableViewCell Class – Hashira Jul 27 '15 at 08:43
  • Is that a custom cell ? – streem Jul 27 '15 at 08:47
  • @Justa Yes,it is a custom cell I create to combine my cell with the controls in it – Hashira Jul 27 '15 at 08:50
  • So it is a `UIButton` inside a custom `UITableViewCell`, correct? – Luca Angeletti Jul 27 '15 at 08:50
  • 2
    Possible duplicates: http://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped, http://stackoverflow.com/questions/25867439/get-the-indexpath-from-inside-a-uitableviewcell-subclass-in-swift, http://stackoverflow.com/questions/29722113/how-to-access-index-path-of-button-in-a-custom-tableviewcell, http://stackoverflow.com/questions/27105948/find-the-indexpath-of-a-button-inside-uitableviewcell-when-button-pressed, ... – Martin R Jul 27 '15 at 08:52
  • @appzYourLife Yes,u r right – Hashira Jul 27 '15 at 08:53
  • check this question dude http://stackoverflow.com/questions/31649220/detect-button-click-in-table-view-ios-xcode-for-multiple-row-and-section – Nischal Hada Jul 27 '15 at 09:19

3 Answers3

4

The best way of doing anything with custom cells is to have a custom class to go along with it.

That way the button is a property of the class and the action can be sent to the cell instead.

That way you can use a block to customise what the button does at the point of creating it.

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

    // create the cell...

    cell.buttonBlock = {
        self.buttonTappedAtIndexPath(indexPath)
    }

    return cell
}

Or something like this.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
4

I also had this problem once, and I did this way.

// CustomCellClass

var tableViewdelegate: MyTableViewDelegateClass?

// button in cell action
@IBAction func buttonPressed(sender: AnyObject) {
    tableViewdelegate?.buttonPressedInCell(cell: self)
}

// MyTableViewDelegateClass

func buttonPressedInCell(cell: CustomCellClass) {
    let indexPath = tableView.indexPathForCell(cell)

    // other logic you have
}

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

    var cell =  // create your cell or deque

    cell.tableViewdelegate = self

    return cell
}
iamandrewluca
  • 3,411
  • 1
  • 31
  • 38
1

I have tried all of the method above but didn't work.Maybe I can't understand the answers well.

But I fixed this problem by my self.

This is what I did:

1、Create a new class inherit UIButton named ‘ForMore’ and declare a var named ‘row’

2、Combine the ForMoreButton to the custom cell with a name ‘ForMoreButton’

3、Assign the indexPath.row to cell.ForMoreButton.row when I create the cell in the method

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

4、In the ViewController my tableview inside, combine the ForMoreButton to this ViewController directly with an action ‘ButtonTapped’ and then we can get the row from (sender as! ForMore).row

Hashira
  • 139
  • 1
  • 9