开发者

Selecting a subview in a custom UITableViewCell in UITableView

开发者 https://www.devze.com 2023-01-07 00:22 出处:网络
I have a UITableView comprised of custom UITableViewCells with some text and a button in them. The cell\'s allowSelection property is set to NO so that I can tap the button without selecting the cell.

I have a UITableView comprised of custom UITableViewCells with some text and a button in them. The cell's allowSelection property is set to NO so that I can tap the button without selecting the cell.

I开发者_开发技巧 am trying to find a way to know which cell in the table was tapped when the button for that cell is pressed. Is there any way to do this???

Many thanks, Brett


Use this if your UIButton is a direct subview of the UITableViewCell ([cell addSubview:button]; or cell.accessoryView = button;)

- (IBAction)buttonTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview;
}

Or if your UIButton is a subview of the UITableViewCell's contentView ([cell.contentView addSubview:button];):

- (IBAction)buttonTapped:(id)sender {
    UIButton *button = (UIButton *)sender;
    UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
}
0

精彩评论

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