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

Posted

技术标签:

【中文标题】以编程方式突出显示 UITableView 单元格【英文标题】:Programmatically highlight UITableView cell 【发布时间】:2013-02-23 18:28:19 【问题描述】:

我有一个 iPad 应用程序,它使用 UISplitViewController(左侧是 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 时会发生什么?) 您确实写过,该单元格已被选中,但在此调用后未突出显示。你是怎么检查的? 我知道该单元格已被选中,因为它的详细视图显示在我的拆分视图的右侧。 谢谢。我试过 setHighlighted:animated: shouldHighlightCell 在我创建新单元格并将其添加到表格视图顶部时设置为 YES。这就是我希望它突出显示的原因,因为新单元格会自动被选中,并且它的详细视图会显示在右侧。 【参考方案1】:

请确保单元格的selectionStyleUITableViewCellSelectionStyleBlue,并且tableView 的allowsSelection 设置为YES

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

【讨论】:

谢谢。我已经检查了这两个值,它们应该是这样的。【参考方案2】:

我经历并尝试了所有这些和其他解决方案,但没有任何乐趣。在我的情况下,问题(让我发疯了 2 小时)如下 - 在我打电话给selectRowAtIndexPath 后不久,我在tableview 上打电话给reloadData。重新加载消除了所有突出显示!当心这个陷阱!随着不必要的数据调用重新加载消失,突出显示按预期发生。

【讨论】:

【参考方案3】:

我还尝试了许多方法来让初始选择显示在我的单选 UITableView 上。最后对我有用的是将初始行的选择推迟到通过在我的 UITableViewController 的 viewDidAppear 中调用它来设置表:

override func viewDidAppear(animated: Bool)

    tableView.selectRowAtIndexPath(indexPathToSelectInitially, animated: false, scrollPosition: .None)

【讨论】:

【参考方案4】:

我找到了这个,它对我有用(也就是调用委托方法 didSelectRowAtIndexPath)

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

PS。我正在使用 UITableViewController。

【讨论】:

【参考方案5】:

我发现使用所有已知的可能性是完全无法解决的。最后,我放弃了很多代码并改用 NSFetchedResultsController 来修复它。 NSFetchedResultsController 是在我最初编写这个应用程序后不久引入的,它极大地简化了将 Core Data 与 UITableViews 一起使用的过程。 https://developer.apple.com/library/ios/documentation/CoreData/Reference/NSFetchedResultsController_Class/index.html

【讨论】:

【参考方案6】:

它获得的单元格边框看起来像分隔符的背景视图。不要更改界面生成器中的默认表格视图设置。确保 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;

【讨论】:

以上是关于以编程方式突出显示 UITableView 单元格的主要内容,如果未能解决你的问题,请参考以下文章

uitableview 单元格突出显示

UITableView 以编程方式调用滑动操作

在 UITableView 中突出显示所选单元格的边框

UITableview 单元格中的按钮不会在快速点击时突出显示

以编程方式选择表格单元格时缺少 UITableview 表格单元格分隔符

UITableView 以编程方式滚动到不存在的单元格