【问题标题】:UITableViewCell background color when highlighted/selected突出显示/选择时的 UITableViewCell 背景颜色
【发布时间】:2016-06-28 01:42:19
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

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

    添加此代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)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)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)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);
        }
    }
    

    希望这些对你有帮助!

    【讨论】:

    • 真的很昂贵,并且使用私有 API (_UITableViewCellSeparatorView)。一年后我们放弃了这种方法——谨慎使用。
    【解决方案2】:

    在 cellForRowAtIndexPath 方法中添加这个,这样当你在单元格中点击而不是更改标签文本颜色时

    cell.textLabel.highlightedTextColor = [UIColor brownColor];
    

    【讨论】:

    • 是的,我知道了,但是在被点击的单元格上没有显示分隔符的部分呢?
    【解决方案3】:

    你可以用这个-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath]; 
        [c.textLabel setTextColor:[UIColor brownColor]];
    }
    

    【讨论】:

      【解决方案4】:

      您可以添加代码cell.selectedBackgroundView = [UIView new]; 来解决这个问题。使用此方法,您不需要cell.selectionStyle = UITableViewCellSelectionStyleNone;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-09
        • 1970-01-01
        • 1970-01-01
        • 2013-01-09
        • 2018-04-04
        相关资源
        最近更新 更多