【发布时间】:2013-06-05 17:15:20
【问题描述】:
如以下代码所示,当 tableview 被拉伸(从不向上滚动)时,NSLog(@"tap is not on the tableview cell") 将始终被调用(我认为 indexPath 将始终为 nil)。但是,当我在节号大于 2 的节标题中点击头像时,NSLog 不会被调用。很奇怪,有人知道这是怎么回事吗?
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
...
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
[avatar addGestureRecognizer:tapGesture];
//avatar is UIImageView and the user interaction is enabled.
[headerView addSubview: aMessageAvatar];
return headerView;
...
}
-(void)handleTapGesture:(UITapGestureRecognizer *)sender
{
CGPoint point = [sender locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
if (!indexPath) {
NSLog(@"tap is not on the tableview cell");
}
}
【问题讨论】:
-
当您单击随后的标题时,您会说
indexPath不是nil。那么,它是什么?顺便说一句,heightForHeaderInSection:返回的是什么?你的avatar的高度是多少?如果您的如果您的标题视图未设置为剪辑子视图,我可以很容易地想象您认为您正在点击标题视图,但该点可能对应于它后面的一个单元格。 -
@Rob 是表头下的行的indexPath。表头高度为48.0f,头像框为(8, 8, 32,32)。
-
这很好奇,但我不确定我是否会为此担心太多。
indexPathForRowAtPoint中的一些特质(即错误)可能无法优雅地处理在部分页眉/页脚中点击的人。这似乎不是一个非常重要的问题,因为indexPathForRowAtPoint显然不是解决您问题的正确方法。为什么要担心您知道是错误解决方案的解决方案中的潜在错误?使用tag是合乎逻辑的解决方案。
标签: ios uitableview nsindexpath