Detecting which UIButton was pressed in a UITableView

前端 未结 26 3378
小蘑菇
小蘑菇 2020-11-22 00:40

I have a UITableView with 5 UITableViewCells. Each cell contains a UIButton which is set up as follows:

- (UITableView         


        
26条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 01:01

    Found a nice solution to this problem elsewhere, no messing around with tags on the button:

    - (void)buttonPressedAction:(id)sender {
    
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:self.tableView];
        NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];
    
        // do stuff with the indexPath...
    }
    

提交回复
热议问题