UIButton* coverButton = [UIButton buttonWithType:UIButtonTypeCustom];
coverButton.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[coverButton addTarget:self action:@selector(clickGesture:event:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:coverButton];
-(void)clickGesture:(id)sender event:(id)event{
//获取点击的位置
NSSet* set = [event allTouches];
UITouch* touch = [set anyObject];
CGPoint point = [touch locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:point];
NSLog(@"point.x: %f point.y : %f",point.x,point.y);
if (point.x < self.view.frame.size.width / 3) {
// NSLog(@"point.x < 1/3");
}
else if(point.x > self.view.frame.size.width / 3 * 2){
// NSLog(@"point.x > 2/3");
}
else{
// NSLog(@"1/3 <point.x < 2/3");
}
NSLog(@"index row:%d", indexPath.row);
}
本文介绍了一种在iOS应用中为TableView的每个单元格添加覆盖按钮的方法,并通过点击事件获取被点击单元格的位置。该方法利用UIButton监听点击事件,并通过UITouch获取触摸点位置,最终确定点击的是哪个单元格。
5343

被折叠的 条评论
为什么被折叠?



