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

8
我有一个使用UISplitViewController的iPad应用程序,左侧为UITableView,右侧为详情视图。当您单击表格视图时,所选单元格将以蓝色突出显示。
但是,在我调用以下方法时,选定的单元格不会突出显示为蓝色:
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

我已经花了数天的时间尝试各种委托方法和技巧,试图在程序中使单元格高亮,就像它被点击一样。但是我无法做到。

我几乎通过以下方法实现了这个目标:

- (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;
}

我知道这是一个奇怪而复杂的解决方法。我不介意,但它甚至不能完全工作。如果所选单元格被滚动到屏幕外,它会失去高亮,而被点击的单元格则在滚动到屏幕外时仍保持高亮。
我对此感到非常困惑。我确信这种解决方法根本不应该必要,必须有一个更简单的解决方案。

你尝试过使用UITableViewCellsetHighlighted:animated:方法吗?此外,在你所包含的代码中,你没有展示实际上是如何使其高亮的。例如,当你设置shouldHighlightCell时会发生什么? - lnafziger
你说过,在这个调用之后,单元格被选中但没有高亮显示。你是如何检查的? - NoilPaw
我知道该单元格被选中,因为其详细视图显示在我分割视图的右侧。 - beev
谢谢。我已经尝试了setHighlighted:animated:方法。 - beev
当我创建新的单元格并将其添加到表视图顶部时,shouldHighlightCell被设置为YES。这就是为什么我希望它被突出显示,因为新单元格会自动选中,并在右侧显示其详细信息视图。 - beev
当我将shouldHighlightCell = NO;移动到tableView:didSelectRowAtIndexPath:时,这个hack的效果更好。这样即使滚动屏幕,单元格仍然保持高亮状态,但是每当点击另一个单元格时,高亮就会消失。现在这个hack做得还不错。但是每当我从数据存储中重新加载表视图的内容时,我就会失去所选单元格。也许我需要重写问题! - beev
6个回答

12
请确保单元格的selectionStyleUITableViewCellSelectionStyleBlue,且tableView的allowsSelection设置为YES
对我而言,方法selectRowAtIndexPath:animated:scrollPosition:运行良好。它可以突出显示所选单元格。

谢谢。我已经检查了这两个值,它们是正确的。 - beev

3

我尝试了所有这些和其他解决方案,但都没有成功。在我的情况下,问题是这样的 - 在我调用 selectRowAtIndexPath 之后不久,我又在 tableview 上调用了 reloadData。那个重新加载就把所有的突出显示都清除了!要注意这个坑!一旦去掉不必要的数据重新加载调用,突出显示就如预期般发生了。


2

我也尝试了许多方法来使我的单选UITableView显示初始选择。最终对我有效的方法是推迟选择初始行,直到表格设置完成,通过在UITableViewController的viewDidAppear中调用它:

override func viewDidAppear(animated: Bool)
{
    tableView.selectRowAtIndexPath(indexPathToSelectInitially, animated: false, scrollPosition: .None)
}

1

我找到了这个方法,对我有用(也就是调用代理方法didSelectRowAtIndexPath)

NSIndexPath *defaultIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:[self tableView] didSelectRowAtIndexPath:defaultIndexPath];

PS. 我正在使用UITableViewController。


0

它获取具有单元格边框外观的背景视图,看起来像分隔符。不要在Interface Builder中更改默认的tableview设置。确保UITableViewCellSelectionStyleNone未设置为selectionstyle。我将粘贴工作代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *kCellIdentifier = @"PlayListCell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier];
    }
    MPMediaPlaylist *playList = [playlistCollection objectAtIndex:indexPath.row];
    cell.textLabel.text = playList.name;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     // cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d Songs",[playList.items count]];
    MPMediaItemCollection *playListMediaCollection = [playlistCollection objectAtIndex:indexPath.row ];

    cell.imageView.image =[UIImage imageWithCGImage:[self getImageForCollection:playListMediaCollection.items]];

    // the main code which make it highlight

    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor colorWithRed:170.0f/255.0 green:170.0f/255.0 blue:170.0f/255.0 alpha:1.0f];
    [bgColorView.layer setBorderColor:[UIColor blackColor].CGColor];
    [bgColorView.layer setBorderWidth:1.0f];
    [cell setSelectedBackgroundView:bgColorView];
    return cell;
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接