OGeek|极客世界-中国程序员成长平台

标题: ios - 突出显示/选择时的 UITableViewCell 背景颜色 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:42
标题: ios - 突出显示/选择时的 UITableViewCell 背景颜色

我希望我的单元格文本颜色在点击时改变,而不是背景颜色。

我希望单元格背景始终为白色,只有文本颜色在选中时发生变化。

我已经看到了很多关于如何做到这一点的答案......

UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor whiteColor];
[cell setSelectedBackgroundView:bgColorView];

...但是创建的 View 在单元格分隔符之上运行。

enter image description here

并且要使用 cell.textLabel.highlightedTextColor = [UIColor brownColor]; 来更改 textColor,我不能有 cell.selectionStyle = UITableViewCellSelectionStyleNone;,所以我需要弄清楚一些事情。



Best Answer-推荐答案


如果你想显示单元格的分隔符,你可能需要这个:

添加此代码:

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
 ...

 cell.selectionStyle = UITableViewCellSelectionStyleDefault;
 cell.textLabel.highlightedTextColor = [UIColor brownColor];

 UIView *backgroudView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, tableView.bounds.size.width, [self tableView:tableView heightForRowAtIndexPath:indexPath] - 2)];
 backgroudView.backgroundColor = [UIColor whiteColor];

 UIView *placeholderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
 placeholderView.backgroundColor = [UIColor clearColor];
 [placeholderView addSubview:backgroudView];
 cell.selectedBackgroundView = placeholderView;

...
}
// These codes are used to show the separatorView when the cell didSelected
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
        for (id obj in cell.subviews) {
           if([obj isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]){
               UIView *view = (UIView *)obj;
               view.hidden = NO;
           }
        }
     };

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    showSeparatorView(cell);
    if (indexPath.row > 0)
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
        showSeparatorView(cell);
    }
...
}

// These codes are used to show the separatorView when the cell didHightlight
- (void)tableViewUITableView *)tableView didHighlightRowAtIndexPathNSIndexPath *)indexPath
{
    void (^showSeparatorView)(UITableViewCell *cell) = ^(UITableViewCell *cell){
        for (id obj in cell.subviews) {
            if([obj isKindOfClass:NSClassFromString(@"_UITableViewCellSeparatorView")]){
                 UIView *view = (UIView *)obj;
                 view.hidden = NO;
            }
        }
    };

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    showSeparatorView(cell);
    if (indexPath.row > 0)
    {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row - 1 inSection:indexPath.section]];
        showSeparatorView(cell);
    }
}

希望这些对你有帮助!

关于ios - 突出显示/选择时的 UITableViewCell 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38065788/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4