繁体   English   中英

将UIButton触摸事件发送到didSelectRowAtIndexPath

[英]Send UIButton touch event to didSelectRowAtIndexPath

我有一个UIButton添加到UITableViewCell

为了减少代码冗余,我想触摸UIButton的事件以使其通过单元格-因此它最终出现在:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

你能告诉我如何吗?

在按钮的操作方法中,您可以从表视图确定行的索引路径,并将其传递给委托方法:

- (IBAction)buttonClicked:(UIButton *)button
{
    CGPoint point = [button center];
    UITableView *table = [self tableView];
    NSIndexPath *indexPath = [table indexPathForRowAtPoint:point];
    [[table delegate] tableView:table didSelectRowAtIndexPath:indexPath];
}

那是个坏主意。 相反,您应该从didSelectRowAtIndexPath:调用相同的函数didSelectRowAtIndexPath:和按钮的操作方法,并将通用代码放入该函数中。 就像是-

- (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,

阿克沙伊

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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