Bronze Challenge Solution


#1

I see nobody posted it yet, so I will do just for the other people.
So, here is my code (I just put this into cellForRowAtIndexPath method of ItemsViewController):

if ([p valueInDollars] > 50) {
		[[cell valueLabel] setTextColor:[UIColor greenColor]];
	}
	else {
		[[cell valueLabel] setTextColor:[UIColor redColor]];
	}

#2

I did the same except used < 50

    if ([p valueInDollars] < 50) {
        cell.valueLabel.textColor = [UIColor redColor];
    } else {
        [[cell valueLabel] setTextColor:[UIColor greenColor]];
    }

I’ve also been playing with dot notation and have found that it’s helpful as xcode will auto complete acceptable actions which helped me find a textColor property.


#3

[quote=“Tiberius”]
I’ve also been playing with dot notation and have found that it’s helpful as xcode will auto complete acceptable actions which helped me find a textColor property.[/quote]

I know, but the authors said it’s a bad habit in obj-c, so I’m trying to avoid it…


#4

At first I thought about simply modifying ItemsViewController.m, but it was my understanding that changing the color of a label should be the responsibility of the view (With all that MVC stuff). So I ended up creating a UIHighlightLabel class subsclassing UILabel.

@implementation UIHighlightLabel

(snip)

- (void)setDollarValue:(int)value {
    [self setText:[NSString stringWithFormat:@"$%d", value]];
    if (value > 50) {
        [self setTextColor:[UIColour redColor]];
    } else if (value < 50) {
        [self setTextColor:[UIColor greenColor]];
    } else {
        [self setTextColor:[UIColor blackColor]];
    }
}

@end

Then in HomepwnerItemCell.h I have:

@property (weak, nonatomic) IBOutlet UIHighlightLabel *valueLabel;

Then in ItemsViewController.m

[[cell serialNumberLabel] setText:[p serialNumber]];
[[cell valueLabel] setDollarValue:[p valueInDollars]];

Presumably, I could have just overridden the setText method, but that would have assumed that the text incoming was an integer value. What do you think? Or did I over think things?