【问题标题】:How to change the blue selection of a UITableViewCell?如何更改 UITableViewCell 的蓝色选择?
【发布时间】:2010-06-29 19:46:11
【问题描述】:

我尝试在自定义 UITableViewCell 子类中覆盖 -selectedBackgroundView 的访问器。但是,当单元格被选中时,单元格会继续使用默认的蓝色选择背景。怎么改?

我实际上很确定这个蓝色选择的东西是 selectedBackgroundView ... 还有什么应该负责这个具有浅色渐变的蓝色背景?奇怪的是,contentView 在上面,虽然它有一个不是 [UIColor clearColor] 的背景颜色,但这个 selectedBackgroundView 似乎仍然发光。真奇怪。

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    您只需将此代码放入“cellForRowAtIndexPath”

    对于禁用单元格选择:(单击单元格时)

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    

    启用单元格:(单击单元格时)

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;(By Default)
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    

    如果您想在 cellForRowAtIndexPath 中提供自定义颜色

    if (cell==nil)
    
    {  
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
     UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
    }
    

    一切顺利。

    【讨论】:

      【解决方案2】:

      听起来你只是想添加

      self.selectionStyle = UITableViewCellSelectionStyleNone;
      

      到您的自定义类的初始化。如果你想做任何繁重的处理,你也可以覆盖 setSelected:animated: 方法。

      【讨论】:

      • 应该是 cell.selectionStyle = UITableViewCellSelectionStyleNone;而不是 self.selectionStyle = UITableViewCellSelectionStyleNone;
      猜你喜欢
      • 2011-02-02
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 2012-05-21
      • 2023-03-06
      • 2013-02-24
      • 2017-05-12
      相关资源
      最近更新 更多