I have a UIButton added to a UITableViewCell
In order to reduce code redundancy, I'd like to touch event of UIButton to fall through the cell - so it ends up in:
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexP开发者_StackOverflowath
Can you tell me how?
In your button's action method, you can determine the index path of the row from the table view and pass that to the delegate method:
- (IBAction)buttonClicked:(UIButton *)button
{
CGPoint point = [button center];
UITableView *table = [self tableView];
NSIndexPath *indexPath = [table indexPathForRowAtPoint:point];
[[table delegate] tableView:table didSelectRowAtIndexPath:indexPath];
}
That's a bad idea. Instead, you should call the same function from didSelectRowAtIndexPath:
& the button's action method & place your common code in that function. Something like-
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//fetch the cell corresponding to this index path
MyCustomCell* cell = (MyCustomCell*)[self cellForRowAtIndexPath:indexPath] ;
[cell foo] ;
}
-(void) buttonClicked:(id)sender //In your custom cell class
{
[self foo];
}
HTH,
Akshay
精彩评论