rson
rson

Reputation: 1465

String Comparison Woes

I have an array of dictionary objects and I am trying to do a simple comparison of the contents of a record inside the dictionary objects. Here is my code

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    int timeIndex = [indexPath indexAtPosition: [indexPath length] - 1];    
    NSString *isAvailable = [NSString stringWithString:[[timesList objectAtIndex: timeIndex] objectForKey: @"Available"]];

    UITableViewCell *cell;

    static NSString *CellIdentifier = @"Cell";
    static NSString *availableIdentifier = @"availableCell";
    static NSString *unavailableIdentifier = @"unavailableCell";   

    NSLog(@"%@", isAvailable);

    switch ([isAvailable isEqualToString:@"true"]) {
        case YES:
            // or you can just use standard cells here
            cell = [tableView dequeueReusableCellWithIdentifier:availableIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            }
            cell.textLabel.textColor = [UIColor greenColor];    
            cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];
            break;

        case NO:
            cell = [tableView dequeueReusableCellWithIdentifier:unavailableIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            }       
            cell.textLabel.textColor = [UIColor redColor];
            cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];

            break;
    }

    return cell;
}

It is logging correctly as I am seeing the values as I scroll down the table view.

I have also tried an if/else

if([isAvailable isEqualToString:@"true"]){
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:availableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.textColor = [UIColor greenColor];    
    cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];
    return cell;
} else {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unavailableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }       
    cell.textLabel.textColor = [UIColor redColor];
    cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];
    return cell;
}

But in both cases, it is acting as if the isEqualToString:@"true" is false and doing the second condition, when it is clearly being logged as true...

Any thoughts?

Upvotes: 0

Views: 249

Answers (1)

Bryan McLemore
Bryan McLemore

Reputation: 6493

This is dry-coded (needs testing), but I think I'd implement it similar to this. Hope it helps.

NSString * const CellIdentifier = @"Cell";
NSString * const availableIdentifier = @"availableCell";
NSString * const unavailableIdentifier = @"unavailableCell"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int timeIndex = [indexPath indexAtPosition:[indexPath length] - 1];        

    NSString *identifier = nil;
    UIColor *color = nil;

    if ([[[timesList objectAtIndex:timeIndex] objectForKey:@"Available"] boolValue]) {
        identifier = availableIdentifier;
        color = [UIColor greenColor];
    } else {
        identifier = unavailableIdentifier;
        color = [UIColor redColor];
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }

    cell.textLabel.textColor = color;        
    cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];

    return cell;
}

Alternative implementation that you may find preferable:

NSString * const CellIdentifier = @"Cell";
NSString * const availableIdentifier = @"availableCell";
NSString * const unavailableIdentifier = @"unavailableCell"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    int timeIndex = [indexPath indexAtPosition:[indexPath length] - 1];        

    BOOL isAvailable = [[[timesList objectAtIndex:timeIndex] objectForKey:@"Available"] boolValue];

    NSString *identifier = isAvailable ? availableIdentifier : unavailableIdentifier;
    UIColor *color = isAvailable ? [UIColor greenColor] : [UIColor redColor];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }

    cell.textLabel.textColor = color;        
    cell.textLabel.text = [[timesList objectAtIndex: timeIndex] objectForKey: @"Time"];

    return cell;
}

Upvotes: 1

Related Questions