【问题标题】:TableView, how change the cell default blue colorTableView,如何更改单元格默认蓝色
【发布时间】:2012-03-15 07:35:31
【问题描述】:

当我点击 UITableViewCell 中的一个单元格时,该单元格默认变为蓝色。 我该如何改变呢? 更改为另一种颜色或在我正在构建的此示例中,完全摆脱颜色。 我有一个表格,它只是一个表格,信息在单元格中,按下时它不会导致任何地方。只是烦人的是,如果按下它会变成蓝色,因为单元格中的文本更难阅读。

【问题讨论】:

标签: iphone xcode uitableview


【解决方案1】:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

方法添加此代码

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

【讨论】:

  • 或者在界面生成器中更改样式以获得相同的效果。
【解决方案2】:

根据我的情况改变uitableviewcell的选择样式有以下几种可能。

  • 这些是可用的默认样式。

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
  • 但是如果你想改变样式,你也可以使用下面的。

    cell.backgroundColor = [UIColor colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha];
    
    cell.backgroundColor = [UIColor colorWithPatternImage:(UIImage *)image];  
    

使用第二种方法,您可以指定背景颜色或图像。

对于你想要实现的 Selected Cell Style,你可以使用这个。

cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:(UIImage *)image] autorelease];

【讨论】:

    【解决方案3】:

    更改属性 selectedBackgroundView 是正确且最简单的方法。我使用以下代码更改选择颜色:

    //设置选择颜色

    UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
    myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
    cell.selectedBackgroundView = myBackView;
    [myBackView release];
    

    或者你可能会使用这个..

    这是我遇到的解决此问题的最有效方法,使用 willDisplayCell 委托方法(这在使用 cell.textLabel.text 和/或单元格时也会处理文本标签背景的白色。 detailTextLabel.text):

    (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... }
    

    当调用此委托方法时,单元格的颜色是通过单元格而不是表格视图控制的,就像您使用时一样:

    (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { ... }
    

    因此,在单元格委托方法的主体中,添加以下代码以交替使用单元格颜色,或者仅使用函数调用使表格的所有单元格具有相同的颜色。

    if (indexPath.row % 2)
    {
            [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
    }
    else [cell setBackgroundColor:[UIColor clearColor]];
    

    【讨论】:

    • 谢谢,如果我想设置任何我想要的颜色,那就完美了。但是对于我的示例来说,第一个答案中的行就足够了,可以去除颜色。 cell.selectionStyle = UITableViewCellSelectionStyleNone;
    【解决方案4】:

    使用以下代码删除单元格选择的默认颜色 :

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    【讨论】:

      猜你喜欢
      • 2012-01-26
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      相关资源
      最近更新 更多