UITableView:更改选定的单元格背景颜色

UITableView: Change selected cell background color

在 tvOS 上 UITableView 导航时,我无法更改白色背景。

出于设计目的,我对整个背景/文本颜色进行了编辑 table,因此选择的白色背景很烦人。

我试过了:

cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];

但它不会更新突出显示的背景颜色。

tvOS 使用焦点显示当前选中的项目。要更改背景颜色,请覆盖单元格中的 didUpdateFocusInContext() 方法 class。

public override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    context.previouslyFocusedView?.backgroundColor = UIColor.clearColor()
    context.nextFocusedView?.backgroundColor = UIColor.blackColor()
}

您必须创建一个 UIView 并将其设置为您的 CellForRowAtIndexPath 中的背景视图

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryCell" forIndexPath:indexPath];
    UIView *bgColorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 405, 60)];

    bgColorView.backgroundColor = [UIColor blackColor];
    [cell setSelectedBackgroundView:bgColorView];

您可以使用 focusStyle 简单地禁用它:

self.focusStyle = UITableViewCellFocusStyle.Custom

这将禁用白色背景,但也会禁用任何默认缩放动画。您应该使用自定义 UITableViewCellFocusStyle.

自行处理