[英]Clicking a custom UITableView section header is invoking didSelectRow: on first row of the section
我在UITableView
上有一个自定义viewForHeaderInSection
- 让我们称之为HeaderCell
( UITableViewCell
一个简单子类),它具有最奇怪的行为:
当我点击部分标题时:
tableView:didSelectRowAtIndexPath:
与该indexPath
的第一个UITableViewCell
的indexPath
。 就像我点击了单元格,但没有。 我正在点击标题。 问题是,如果我用一个简单的UIView
替换viewForHeaderInSection
上的自定义HeaderCell
,那不会再发生了! 我检查了该xib及其类上链接的任何操作,找不到任何addTarget addTarget:
或任何xib操作。
其他奇怪的因素:它不会发生在第一个 HeaderCell(第 0 部分)上,只会发生在 >= 1 的部分。
在HeaderCell
类上,我只需要实现:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
return;
}
这是最后的解决方案。 以前有人检测过这种行为吗?
您不应该将UITableViewCell
子类用于页眉/页脚。 使用UITableViewHeaderFooterView
(如果您可以使用 >= iOS6)或仅使用UIView
否则。
如果您使用UITableViewCell
作为页眉/页脚,请使用return cell.contentView
而不是return cell
您可以通过 setEnableUserInteraction 方法来完成。 请参考给出的代码,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
[cell setUserInteractionEnabled:YES];
if([indexPath row] == 0)
[cell setUserInteractionEnabled:NO];
return cell;
}
希望对你有帮助 :)
这是一个旧线程,但我刚刚遇到了这个问题和另一种可能的方法来避免这种情况,如果您使用 UITableViewCell 的子类作为标题,那么只需返回内容视图而不是 headerforView 函数中的完整单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.