繁体   English   中英

单击自定义 UITableView 部分标题会调用 didSelectRow: 在该部分的第一行

[英]Clicking a custom UITableView section header is invoking didSelectRow: on first row of the section

我在UITableView上有一个自定义viewForHeaderInSection - 让我们称之为HeaderCellUITableViewCell一个简单子类),它具有最奇怪的行为:

  • 当我点击部分标题时

    • 它触发tableView:didSelectRowAtIndexPath:与该indexPath的第一个UITableViewCellindexPath 就像我点击了单元格,但没有。 我正在点击标题。

问题是,如果我用一个简单的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM