1

I subclassed UITableViewCell and added a round rec button using IB.

Normally, the event will be handled on the class of my custom UITableViewCell.

But how do handle the event that the button is clicked on the viewController which has a UITableView that uses this UITableViewCell?

Gavin
  • 2,784
  • 6
  • 41
  • 78

2 Answers2

0

When creating the custom cell, make sure you are adding the view controller as the button action's target. So in your view controller's (assuming it is the datasource for the table view):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // All your standard identifier & dequeueing stuff here
    if (cell == nil) 
    {
        // We are creating a new cell, let's setup up its button's target & action 
        cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        [cell.yourButton addTarget:self action:@selector(yourAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}
Rog
  • 18,602
  • 6
  • 76
  • 97
  • This works everytime the button is tapped. But how do I pass the the indexpath it was tapped on to the selector? – Gavin Sep 26 '11 at 02:56
  • I have solved the issue with references from http://stackoverflow.com/questions/7044532/pass-parameter-through-selector – Gavin Sep 26 '11 at 03:18
  • You can grab a reference to the table view cell by calling super super (that is twice) on the button. Then it is just a matter of using the tableview's indexpathforcell: method. – Rog Sep 26 '11 at 03:38
0

So its kinda akward the way u are doing it because what u really wanna do is create a subclass of uiview and then create that and add it to the contentView of the cell. But since u are doing it this way u should set the tag of the button to its index and then in the selector have the function say cancelButton: and then this should be ur cancelButton function decleration -(void)cancelButton:(id)sender { UIButton pressed = (UIButton)sender; //and then pass in the tag pf the button with pressed.tag }

Tony
  • 4,609
  • 2
  • 22
  • 32