如何更改UITableViewCell的蓝色突出显示颜色?

Tho*_*oos 126 uitableview ios

我想知道如何改变UITableViewCell任何想法的蓝色突出显示/选择颜色?

zon*_*ble 209

您可以通过多种方式更改突出显示颜色.

  1. 更改单元格的selectionStyle属性.如果将其更改为UITableViewCellSelectionStyleGray,则为灰色.

  2. 改变selectedBackgroundView财产.实际上创建蓝色渐变的是一个视图.您可以创建视图并绘制您喜欢的内容,并将视图用作表格视图单元格的背景.

  • 谢谢你的好评!这使得它现在变得如此简单:你创建一个新视图(甚至不需要指定框架:`cell.selectedBackgroundView = [UIView new];`并设置你想要的任何颜色:`cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHex :@"ecf2f5"和Alpha:1];` (12认同)
  • 很酷,在这里找到了一个很好的解释和例子:http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html (8认同)

T D*_*vey 144

Zonble已经提供了一个很好的答案.我认为包含一个简短的代码片段来添加UIView到将作为选定背景视图显示的tableview单元格可能会很有用.

cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    UIView *selectionColor = [[UIView alloc] init];
    selectionColor.backgroundColor = [UIColor colorWithRed:(245/255.0) green:(245/255.0) blue:(245/255.0) alpha:1];
    cell.selectedBackgroundView = selectionColor;
Run Code Online (Sandbox Code Playgroud)
  • 细胞是我的 UITableViewCell
  • 我创建了一个UIView并使用RGB颜色设置其背景颜色(浅灰色)
  • 然后我设置单元格selectedBackgroundViewUIView我创造了我的选择背景颜色

这对我很有用.感谢Zonble提示.

  • @kirk,你的意思是分组:) (7认同)
  • 使用带有节的表视图时,这不能很好地工作.选中后,单元格将丢失边框,并且在第一个或最后一个单元格时不会舍入. (2认同)

Ars*_*wez 30

UITableViewCell 有三种默认选择样式: -

  1. 蓝色
  2. 灰色
  3. 没有

实施情况如下: -

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

     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];       
}
Run Code Online (Sandbox Code Playgroud)

  • 而不是这三种默认样式,我们也可以通过selectedBackgroundView属性自定义我们期望的样式. (7认同)

Nat*_*bot 18

如果要在应用程序范围内更改它,可以将逻辑添加到App Delegate

class AppDelegate: UIResponder, UIApplicationDelegate {

    //... truncated

   func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

        // set up your background color view
        let colorView = UIView()
        colorView.backgroundColor = UIColor.yellowColor()

        // use UITableViewCell.appearance() to configure 
        // the default appearance of all UITableViewCells in your app
        UITableViewCell.appearance().selectedBackgroundView = colorView

        return true
    }

    //... truncated
}
Run Code Online (Sandbox Code Playgroud)


ali*_*ner 15

在Swift中,使用它 cellForRowAtIndexPath

let selectedView = UIView()
selectedView.backgroundColor = .white
cell.selectedBackgroundView = selectedView
Run Code Online (Sandbox Code Playgroud)

如果您希望每个选择颜色相同UITableViewCell,请使用此选项AppDelegate.

let selectedView = UIView()
selectedView.backgroundColor = .white
UITableViewCell.appearance().selectedBackgroundView = selectedView
Run Code Online (Sandbox Code Playgroud)


Mar*_*fer 11

为了完整性:如果您创建了自己的子类,则UITableViewCell可以实现该- (void)setSelected:(BOOL)selected animated:(BOOL)animated方法,并设置您在内容视图中添加的某个视图的背景颜色.(如果是这种情况)或contentView本身(如果它没有被您自己的一个视图覆盖.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if(selected) {
        self.contentView.backgroundColor = UIColor.blueColor;
    } else {
        self.contentView.backgroundColor = UIColor.whiteColor;
    }
}
Run Code Online (Sandbox Code Playgroud)

(没用?适合小宽度的源代码DIV :)

这种方法比使用selectedBackgroundView有两个优点,它使用更少的内存和更少的CPU,除非你显示数百个单元格,否则你甚至不会注意到.


sam*_*ize 10

我必须将选择样式设置UITableViewCellSelectionStyleDefault为自定义背景颜色才能工作.如果是任何其他样式,将忽略自定义背景颜色.在iOS 8上测试过.

单元格的完整代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // This is how you change the background color
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    UIView *bgColorView = [[UIView alloc] init];
    bgColorView.backgroundColor = [UIColor redColor];
    [cell setSelectedBackgroundView:bgColorView];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)


小智 5

@IBDesignable class UIDesignableTableViewCell: UITableViewCell {
  @IBInspectable var selectedColor: UIColor = UIColor.clearColor() {
    didSet {
      selectedBackgroundView = UIView()
      selectedBackgroundView?.backgroundColor = selectedColor
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在故事板中,将 UITableViewCell 的类设置为 UIDesignableTableViewCell,在属性检查器上,您可以将单元格的选定颜色更改为任何颜色。

您可以将其用于所有单元格。这就是您的属性检查器的外观。

这就是你的属性检查器的样子


Sam*_*ing 5

斯威夫特3.0 / 4.0

如果您创建了自己的自定义单元格,则可以更改awakeFromNib()所有单元格的选择颜色:

 override func awakeFromNib() {
    super.awakeFromNib()

    let colorView = UIView()
    colorView.backgroundColor = UIColor.orange.withAlphaComponent(0.4)

    self.selectedBackgroundView = colorView


}
Run Code Online (Sandbox Code Playgroud)


Cli*_*ize 5

根据@user的回答,您可以将此扩展名添加到应用程序代码中的任何位置,并直接在故事板编辑器中为应用程序的每个单元格选择颜色:

@IBDesignable extension UITableViewCell {
    @IBInspectable var selectedColor: UIColor? {
        set {
            if let color = newValue {
                selectedBackgroundView = UIView()
                selectedBackgroundView!.backgroundColor = color
            } else {
                selectedBackgroundView = nil
            }
        }
        get {
            return selectedBackgroundView?.backgroundColor
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

故事板中的UITableViewCell选择颜色