KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
What is best practice to handle a button touch event for a button of a custom UITableViewCell ? my classes: MyViewController , MyCustomCell I can think of three options: First option- Have the button as a property of MyCustomCell , and then add a target to it in the MyViewController .m file with MyViewController as the target. MyViewController .m file - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"customCell"; MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; [cell.theButton addTarget:self action:@selector(theButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; } // Configure the cell... [self configureCell:cell atIndexPath:indexPath]; return cell; } - (void)theButtonTapped:(UIButton *)sender { MyCustomCell *selectedCell = (MyCustomCell *)sender.superview; if (selectedCell) { NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell]; MyModel *selectedModel = [self.model objectAtIndex:indexPath.row]; // do something with the model... } } Second option- If the custom cell was made in IB, Set the nib File's Owner to be MyViewController , implement buttonTapped: method in MyViewController and connect the button's Touch Up Inside event to the buttonTapped: method. Third option- if the custom cell wasn't made in IB, add a target to the button in the MyCustomCell .m file with <cod
Tags (comma-separated)
Save Edits
Cancel