gpt4 book ai didi

ios - 在触摸 tableView 时检测单元格索引(但不是页眉/页脚)

转载 作者:行者123 更新时间:2023-11-29 01:21:16 24 4
gpt4 key购买 nike

我有一个包含 tableView 的 UITableViewController。我已经向这个 Controller 添加了一个轻击手势识别器,我希望当用户点击除了 tableView 的第一行(使用 indexPath.row == 0 )以外的任何地方时都会发生 Action 。

我看到了以下相关问题和答案(以及其他):

  • this one关注的是检测 tableView 本身(因此它也会检测它的页眉/页脚)。

  • 我用过this one (下面的代码)之前,但问题是它计算了 (0,0) 的 indexPath如果点击页脚/页眉。

.

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gesture shouldReceiveTouch:(UITouch *)touch {   
CGPoint buttonPosition = [touch locationInView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath.row == 0) {
return NO; // will also be returned if I click on the header / footer
}
return YES;
}

最佳答案

也许尝试在表格 View 单元格上添加一个“不可见”的 UIButton,然后点击时使用按钮标签作为索引。就像下面的伪代码:

-cellForRowAtIndexPath:index {
UIButton *button = [UIButton alloc]initWithFrame:cell.frame];
[button setTag:index.row];
[button addTarget:self action:@selector(getIndex:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview: button]
}


-(void)getIndex:(UIButton *)sender {
finalIndex = [sender tag];
}

关于ios - 在触摸 tableView 时检测单元格索引(但不是页眉/页脚),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com