2009-09-28 125 views
24

我需要将表格视图的默认蓝色选择更改为某种自定义颜色。有没有办法做到这一点。帮我需要更改表格视图选择的颜色

+12

我想你会得到更好的结果,如果你添加更多的惊叹号... – 2009-10-30 12:38:24

回答

62

做到这一点,最好的办法是这样的:

// 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; 
} 

对您的相关部分是cell.selectedBackgroundView = v;指令。 你可以在这里用你喜​​欢的任何视图替换非常基本的视图'v'。

+1

卓然,我已经验证了这种方法适用于普通表格视图,但它不适用于我的分组表格。任何解决方法?或者你可以证实这一点? – 2009-09-28 13:07:28

+0

它不依赖于表视图类型:我刚刚发现它可以在一些普通表上使用,并且不会在其他普通表上使用。他们都使用自定义表格单元格。 – 2009-09-28 13:22:23

+1

这只适用于普通表格视图无模糊。对于分组表格视图,实际需要4个视图来选择不同的背景;最顶层,最底层,在另外两个单元之间,以及组中的单个单元。然后更新这些更改您在任何部分中更改行数的痛苦。在bugreport.apple.com上发布功能请求,为iPhone OS 4.0添加“selectedBackgroundGradient”属性。 – PeyloW 2009-09-28 14:56:05

3

我不认为你可以使用自定义颜色。但是,你可以使用的UITableViewCell

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle 

选择的风格是backgroundView常数,确定细胞的颜色选择,当它的下列财产。默认值是UITableViewCellSelectionStyleBlue。由于

typedef enum { 
    UITableViewCellSelectionStyleNone, 
    UITableViewCellSelectionStyleBlue, 
    UITableViewCellSelectionStyleGray 
} UITableViewCellSelectionStyle; 

您可以从默认的蓝色切换到灰色,或根本没有彩色选择。

+1

如果你继承你的UITableViewCell,你可以突出显示任何颜色。 – mahboudz 2009-09-28 07:10:58

+0

您是如何为使用IB或编程设计的自定义UITableViewCell执行此操作的?我的意思是,是否有任何方法/属性允许这样做,而不会重复使用诸如将另一视图放在单元格之上的技巧? – 2009-09-28 07:59:24

+0

你可以给我发送UITableViewCell子类的代码并用自定义颜色突出显示它。 please .. – smakstr 2009-09-28 08:42:28

0

这样做的另一种方法是在你的单元格中移动一个新的视图,使用任何你想要的颜色和50%左右的不透明度。当您收到-setSelected:animated:call时,将此视图移至单元格。当我说移动时,你实际上可以始终在单元格的顶部有一个视图,但只需将隐藏位关闭并根据需要打开即可。

6

我也遇到过这个问题,试图为分组的单元格创建自定义的选定单元格视图。我通过为细胞顶部,中间和底部创建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; 

有可能是一种更简单的方法,但我很满意的结果。

+0

据我所知,这正是要做到这一点。看到这个[教程](http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html)。你已经忘记了“top_bottom”的变化(当只有一个单元格时),就是这样。 – Rich 2012-02-01 16:58:04

3

确保您在头后声明

@property(nonatomic) UITableViewCellSelectionStyle selectionStyle 

实施

cell.selectionStyle = UITableViewCellSelectionStyleGray 

其中UITableViewCellSelectionStyleGray可以UITableViewCellSelectionStyleNoneUITableViewCellSelectionStyleBlueUITableViewCellSelectionStyleGray