简体   繁体   English

需要更改表格视图选择的颜色

[英]Need to change the color of table view selection

I need to change the default blue color selection of table view to some custom color. 我需要将表格视图的默认蓝色选择更改为某些自定义颜色。 Is there any way to do that. 有没有办法做到这一点。 Help me 帮我

The best way to do this is like this: 最好的方法是这样的:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"myCellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *v = [[[UIView alloc] init] autorelease];
        v.backgroundColor = [UIColor redColor];
        cell.selectedBackgroundView = v;
    }
    // Set up the cell...
    cell.textLabel.text = @"foo";
    return cell;
}

The relevant part for you is the cell.selectedBackgroundView = v; 与您相关的部分是cell.selectedBackgroundView = v; instruction. 指令。 You can substitute the very basic view 'v' here with any view you like. 您可以在此处用任何您喜欢的视图替换基本视图“ v”。

I also came across this problem with trying to create custom selected cell views for the grouped cell. 我还尝试为分组的单元格创建自定义选定的单元格视图时遇到了此问题。 I solved this problem by creating 3 types of images for the cell top, middle and bottom assuming that there will be a middle cell. 我通过为单元格顶部,中间和底部创建3种类型的图像(假设存在一个中间单元格)解决了这个问题。

NSString *imageFile;

if (row == 0) {
 imageFile = @"highlighted_cell_top.png";
} else if (row == ([registeredDetailsKeys count] - 1)) {
 imageFile = @"highlighted_cell_bottom.png";
} else {
 imageFile = @"highlighted_cell_middle.png";
}

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageFile]];

cell.selectedBackgroundView = imageView;

There is possibly an easier way but I am happy with the result. 可能有一种更简单的方法,但我对结果感到满意。

I do not think you can use a custom color. 我认为您不能使用自定义颜色。 However, you can use the following property of UITableViewCell 但是,您可以使用UITableViewCell的以下属性

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle

The selection style is a backgroundView constant that determines the color of a cell when it is selected. 选择样式是backgroundView常量,用于确定选定单元格时的颜色。 The default value is UITableViewCellSelectionStyleBlue. 默认值为UITableViewCellSelectionStyleBlue。 Since 以来

typedef enum {
   UITableViewCellSelectionStyleNone,
   UITableViewCellSelectionStyleBlue,
   UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;

you can switch from the default blue to gray, or no colored selection at all. 您可以从默认的蓝色切换为灰色,或者根本不选择彩色。

Make sure that after you declare in the header 确保在标题中声明后

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle

Implement 实行

cell.selectionStyle = UITableViewCellSelectionStyleGray

where UITableViewCellSelectionStyleGray can be UITableViewCellSelectionStyleNone , UITableViewCellSelectionStyleBlue , UITableViewCellSelectionStyleGray . 其中, UITableViewCellSelectionStyleGray可以是UITableViewCellSelectionStyleNoneUITableViewCellSelectionStyleBlueUITableViewCellSelectionStyleGray

Another way to do it would be to move in a new view over your cell, with whatever color you'd like and 50% or so opacity. 另一种方法是在您的单元格上移动一个新视图,使用您想要的任何颜色和50%左右的不透明度。 You move this view over to the cell when you get a -setSelected:animated: call. 当您收到-setSelected:animated:调用时,可以将此视图移到单元格上。 When I say move, you actually could always have a view on top of your cell, but just turn the hidden bit off and on as you need. 当我说移动时,实际上您总是可以在单元格的顶部看到一个视图,但是只需根据需要关闭和打开隐藏的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM