【问题标题】:How to change the selected cell background color without missing the text如何在不丢失文本的情况下更改选定的单元格背景颜色
【发布时间】:2014-12-06 17:25:54
【问题描述】:

我想更改所选单元格的背景颜色并将文本保留在该单元格上。然后我就这样做了,它可以改变单元格颜色但单元格上的文本会消失。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MessageCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.receivedData objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"MessageCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIView *bgColorView = [[UIView alloc]init];
    bgColorView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];

    cell.selectedBackgroundView = bgColorView;

}

【问题讨论】:

标签: ios uitableview colors background


【解决方案1】:

您正在用一个新的 UIView 实例(其中没有文本,但是您的新颜色)替换当前视图(其中包含您的文本的视图)。这就是你的文字消失的原因。您应该像这样更改 selectedBackgroundview 的背景颜色属性:

cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:0.529 green:0.808 blue:0.922 alpha:0.5];

虽然我认为你甚至可以放弃 selectedBackgroundView 部分,只在 didSelectRowAtIndexPath 中执行 cell.backgroundColor = ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2015-08-28
    • 2014-06-16
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多