突出显示/选中时的UITableViewCell背景颜色

时间:2016-06-28 01:42:20

标签: ios objective-c uitableview

我想要在点击时更改单元格文本颜色,而不是背景颜色。

我希望单元格背景始终为白色,并且只选择要更改的文本颜色。

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

onResume

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

enter image description here

要使用UIView *bgColorView = [[UIView alloc] init]; bgColorView.backgroundColor = [UIColor whiteColor]; [cell setSelectedBackgroundView:bgColorView]; 更改textColor,我无法cell.textLabel.highlightedTextColor = [UIColor brownColor];,所以我需要弄明白。

4 个答案:

答案 0 :(得分:3)

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

添加以下代码:

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

希望这些可以帮到你!

答案 1 :(得分:0)

在cellForRowAtIndexPath方法中添加此项,以便在单击单元格而不是更改标签文本颜色时

cell.textLabel.highlightedTextColor = [UIColor brownColor];

答案 2 :(得分:0)

你可以使用它 -

def reverse(n):  

   j=0  

while(n!=0):  

        j=j*10  

        j=j + (n%10)  

        n=n/10  

print(j)  

reverse(45)

答案 3 :(得分:0)

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

相关问题