如何设置表格行的自定义颜色

时间:2010-10-20 12:32:54

标签: iphone uitableview colors

如何在单击表格行时设置自定义颜色...

我的意思是我不想要默认出现的那种蓝色......

3 个答案:

答案 0 :(得分:2)

要获得您想要的任何颜色,您必须用您自己的UIView替换selectedBackgroundView。

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellID";
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIView *selectedBackground = [[[UIView alloc] init] autorelease];
        selectedBackground.backgroundColor = [UIColor magentaColor];
        cell.selectedBackgroundView = selectedBackground;
    }
    // configure cell
    return cell;
}

答案 1 :(得分:1)

您可以设置tableView单元格属性

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

答案 2 :(得分:1)

创建自己的UITableViewCell子类。覆盖方法-(void)setSelected:(BOOL)selected

这样的事情:

- (void)setSelected:(BOOL)selected {
       [super setSelected:selected];
       if (selected) {
             self.backgroundColor = [UIColor greenColor];
       } else {
             self.backgroundColor = [UIColor whiteColor];
       }
}