0

I am making an iOS app that relies on a table view. In each cell of the table view, there are 4 buttons aligned on the bottom. I have a cell class that is pretty standard and a feedController to handle the table and setting all the items of the cell.

Everything works fine on it but I can not figure out how to handle the button clicks within the cell. I can hard code it into my cell class, but then every 3 cells has the same interaction. Is there a way to pass the button click function from the cell class into the controller? I have tried checking the state from the controller and that has not worked.

gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • Use addTarget:action:forControlEvents: in cellForRowAtIndexPath, and set the target to self (which will be the controller), then implement the button's action method in the controller. – rdelmar Mar 01 '15 at 22:02
  • The problem with addTarget:action:forControlEvents approach is that the action only receives the button, so you do not know which cell . You are also hard coding an understanding of how the cell presents data in the table view controller. Better to have your cell provide block or delegate calls you can control what information the cell can pass on. Or just pass the cell itself in the block/delegate. See my answer. – Rory McKinnel Mar 02 '15 at 11:24

3 Answers3

0

Can you add a gesture recognizer as you're doing your cellForItemAtIndexPath? So I had something similar with a collection view, and what I did was as it within:

 func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
    {
        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as MyCollectionView
...

I would add a gesture recognizer to each cell

i.e.

cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector("tapAction:")))

And then something like:

 func tapAction(recognizer: UITapGestureRecognizer) {
   ...
}

so recognizer ends up being the specific item tapped, and I could take action accordingly (in my case, I had my datasource of items and I would find the item in an array by casting recognizer to a cell, finding the appropriate subview, and update values on it)

NullHypothesis
  • 4,286
  • 6
  • 37
  • 79
0

I would add code block properties to your cell class which the table can assign to deal with each button. In your cell, code each button handler to call the appropriate block, or pass an index for the button used in a single block.

See my answer here which has an example, but for a switch.

How can I get index path of cell on switch change event in section based table view

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • I like this method and I think it is the correct way, I am just not sure how to do this using swift because it does not have @property :/ I read something about closures but Im not too sure how to get them to work with this – Justin Ruoff Mar 08 '15 at 03:07
  • Seems swift has no properties and that 'var' is the equivalent. See here: http://stackoverflow.com/questions/24014800/property-synthesize-equivalent-in-swift .So just assign the blocks to a var and you are off and running. – Rory McKinnel Mar 08 '15 at 09:45
0

If after a few cells you get the same interaction, it's possibly because you're dequeueing a reusable cell, and you're getting the same cell. Make sure to set your .setTarget() call for your buttons in your tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) data source every time the cell is dequeued. It would help if you shared how you're handling dequeuing to see if this is your issue.

raf
  • 2,569
  • 20
  • 19