触摸时在UITableView截面标题上显示选择突出显示

Gau*_*rma 2 objective-c uitableview ios

默认情况下,在UITableView上,当您触摸单元格时,它将进入选定模式,并以灰色突出显示.

在我们的应用程序中,您可以触摸标题,然后转到另一个页面.

我们希望用户在触摸某个部分时获得相同类型的视觉反馈,就像他们触摸一个单元格时一样.也就是说,它通过进入"选定"模式并使背景变得更暗来指示选择来响应.

触摸UITableView部分时不会发生这种情况.该部分不进入选择模式.它不会变灰.

我们尝试过一个透明按钮,它跨越了一个带有背景图像的自定义UIView,并启用了"逆转突出显示",但结果不是非常敏感而且很糟糕.

实现此类功能的最佳策略是什么?

Sto*_*nz2 5

我建议将UIGestureRecognizer子类化以获取touchDown事件,如本文所述:Touch Down Gesture

然后,您可以将手势识别器添加到您的输入UITableViewHeaderFooterView中,viewForHeaderInSection并在其触发时,抓取"选定单元格"的索引路径.null由于某种原因,它将用于第一部分,但indexPath.section对于所有其他部分将是正确的:

CGPoint point = [touchDown locationInView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
Run Code Online (Sandbox Code Playgroud)

然后只需更改标题单元格的背景颜色.当您点击另一个标题或单元格或其他任何内容时,您可能想要清除标题上的颜色,因此每当您更改其背景时,请按住单元格:

// declare an instance UITableViewHeaderFooterView earlier
if(header)
    [[header contentView] setBackgroundColor:[UIColor lightGrayColor]];  // or whatever your default is

if(indexPath)
{
    header = [self.tableView headerViewForSection:indexPath.section];
    [[[self.tableView headerViewForSection:indexPath.section] contentView] setBackgroundColor:[UIColor greenColor]];  // or whatever color you want
}
else
{
    header = [self.tableView headerViewForSection:0];
    [[[self.tableView headerViewForSection:0] contentView] setBackgroundColor:[UIColor greenColor]];
}
Run Code Online (Sandbox Code Playgroud)