开发者

Send UIButton touch event to didSelectRowAtIndexPath

开发者 https://www.devze.com 2023-03-29 13:28 出处:网络
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:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消