ios - 以编程方式突出显示 UITableView 单元格

标签 ios ipad uitableview uisplitviewcontroller

我有一个使用 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;
}

我知道这是一个奇怪而复杂的解决方法。我不介意,但它甚至不能完全工作。选定的单元格在滚动出屏幕时会失去其突出显示,而已点击的单元格在滚动出屏幕时仍会突出显示。

我对此感到非常困惑。我敢肯定这种解决方法甚至不是必需的,因为有一个更简单的解决方案。

最佳答案

请确定单元格的selectionStyleUITableViewCellSelectionStyleBlue和 tableView 的 allowsSelection设置为 YES .

方法selectRowAtIndexPath:animated:scrollPosition:对我来说很好用。它确实突出显示了选定的单元格。

关于ios - 以编程方式突出显示 UITableView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15044018/

相关文章:

iphone - 与每个细胞独特合作的一种观点

iphone - 分隔线之前的空白进入我的 TableView

ios - 将所有内容更改为黑白(应用混合以查看背景颜色)

php - 在 PHP 中为 iPad 用户代理加载 css

objective-c - 分离静态 NSString 以加载到数组中

iPhone分组uitableview标题字体样式

ios - NSURLSession 的 NSURLCache 没有按预期工作

iphone - 如何以编程方式增加 UITableView 中单元格的高度?

iphone - 在 iOS 应用程序中打开网页

ios - UITableViewCell 图像复制