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

Iphone 如何设置表格行的自定义颜色,iphone,uitableview,colors,Iphone,Uitableview,Colors,如何设置单击表格行时表格行的自定义颜色 我的意思是我不想要默认显示的蓝色…您可以设置tableView单元格属性 cell.selectionStyle = UITableViewCellSelectionStyleGray; 可以设置tableView单元格属性 cell.selectionStyle = UITableViewCellSelectionStyleGray; 要获得所需的任何颜色,您必须用自己的UIView替换所选的BackgroundView - (UITableVi

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


我的意思是我不想要默认显示的蓝色…

您可以设置tableView单元格属性

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

可以设置tableView单元格属性

cell.selectionStyle = UITableViewCellSelectionStyleGray; 

要获得所需的任何颜色,您必须用自己的UIView替换所选的BackgroundView

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

要获得所需的任何颜色,您必须用自己的UIView替换所选的BackgroundView

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

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

大概是这样的:

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

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

大概是这样的:

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

+1:好的,但问题是如何在我的类中使用它,这个类已经有了tableview,并且是UIViewController的子类…实际上是UITableViewCell的一个从未使用过的子类…所以请给出相应的想法…+1:好的,但问题是如何在我的类中使用它,这个类已经有了tableview,并且是UIViewController的子类UIViewController…实际上是UITableViewCell的一个从未使用过的子类…所以请给出相应的想法…是的,这在分组样式中不起作用。使用分组样式实现同样的目标要困难得多。一种方法是将UIView子类化,就像“是”中所做的那样,这在分组样式中不起作用。使用分组样式实现同样的目标要困难得多。一种方法是将UIView子类化,如中所做的