我有一个使用 UISplitViewController 的 iPad 应用程序(左侧是 UITableView,右侧是详细 View )。当您点击所选单元格时,我的表格 View 会将其突出显示为蓝色。
当我调用以下方法时,单元格被选中但未以蓝色突出显示:
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
我花了好几天时间摆弄各种委托(delegate)方法和技巧,试图让单元格以编程方式突出显示,就像它被点击一样。我做不到。
我几乎已经做到了这一点:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (shouldHighlightCell)
{
NSIndexPath *indexPathForCellToHighlight = [NSIndexPath indexPathForRow:0 inSection:0];
if ([indexPath isEqual:indexPathForCellToHighlight])
{
cell.selected = YES;
shouldHighlightCell = NO;
}
}
}
只要我也有这个它就可以工作(否则即使点击另一个单元格它也会保持选中状态):
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
if ([[self.tableView cellForRowAtIndexPath:ip] isSelected])
{
[[self.tableView cellForRowAtIndexPath:ip] setSelected:NO];
}
NSIndexPath *iToTheP = indexPath;
return iToTheP;
}
我知道这是一个奇怪而复杂的解决方法。我不介意,但它甚至不能完全工作。选定的单元格在滚动出屏幕时会失去其突出显示,而已点击的单元格在滚动出屏幕时仍会突出显示。
我对此感到非常困惑。我敢肯定这种解决方法甚至不是必需的,因为有一个更简单的解决方案。
最佳答案
请确定单元格的selectionStyle
是UITableViewCellSelectionStyleBlue
和 tableView 的 allowsSelection
设置为 YES
.
方法selectRowAtIndexPath:animated:scrollPosition:
对我来说很好用。它确实突出显示了选定的单元格。
关于ios - 以编程方式突出显示 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15044018/