简体   繁体   中英

Send UIButton touch event to didSelectRowAtIndexPath

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 *)indexPath

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM