UITableViewCell Background Color Problem

I have subclassed UITableViewCell to set the background color of the cell to the color I need:

.h

@interface DataViewCustomCell : UITableViewCell {
    UIColor* cellColor;
    UIColor* standardColor;
}
- (void) setCellColor: (UIColor*)color;

@end

.m

@implementation DataViewCustomCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void) setCellColor: (UIColor*)color
{
    cellColor = color;
}

- (void) spreadBackgroundColor: (UIView*)that withColor: (UIColor*)bkColor
{
    NSEnumerator *enumerator = [that.subviews objectEnumerator];
    id anObject;

    while (anObject = [enumerator nextObject]) {
        if([anObject isKindOfClass: [UIView class]])
        {
            ((UIView*)anObject).backgroundColor = bkColor;
            [self spreadBackgroundColor:anObject withColor:bkColor];
        }
    }
}

- (void) layoutSubviews {
    [super layoutSubviews]; // layouts the cell as UITableViewCellStyleValue2 would normally look like

    if(!self.selected && NULL != cellColor)
    {
        [self spreadBackgroundColor:self withColor:cellColor];
    }
}

- (void)dealloc
{
    [super dealloc];
}

@end

When I call setCellColor with the color I want, everything goes fine, but when I did not find a way to set the original color back: when I set [UIColor clearColor]with type UITableViewStylePlain, the results do not look beautiful.

Wrong result

How can I achieve a good result without a cell separator dividing line?

+3
source share
6 answers
Finally, I decided it myself. I used the following code in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathplace of a subclass:
    UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
    bg.backgroundColor = [UIColor greenColor]; //The color you want
    cell.backgroundView = bg;
    cell.textLabel.backgroundColor = bg.backgroundColor;
    [bg release];
+8
source

, edo42. , , . , : UITableViewCellStyleSubtitle.

, , :

BackgroundColor UITableViewCellStyleSubtitle? BackgroundColor UITableViewCellStyleSubtitle?

:

, tableView: willDisplayCell: forRowAtIndexPath: tableView: cellForRowAtIndexPath: , :

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    cell.backgroundColor = [UIColor whiteColor];
}
+10

, UIColor. UIColor * originalColor = anObject.backgroundColor; , , originalColor.

0

, .m, tableViewController. , . :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

. , , . :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
     self.backgroundColor = [UIColor greenColor];
     [super touchesBegan:touches withEvent:event];
}

, , touchEnded, .

0

you should always indicate the changes for the cell in the willDisplayCell: forRowAtIndex file instead of the cellForRowAtIndex method, according to Apple documentation

It works!!

0
source

Change the color in the following delegate method:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (...){
        cell.backgroundColor = [UIColor blueColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
    }
}
0
source

All Articles