Горе сравнения строк - PullRequest
       5

Горе сравнения строк

0 голосов
/ 17 ноября 2009

У меня есть массив объектов словаря, и я пытаюсь сделать простое сравнение содержимого записи внутри объектов словаря. Вот мой код

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

Ведение журнала корректно, так как я вижу значения при прокрутке таблицы вниз.

Я также пробовал 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;
}

Но в обоих случаях он действует так, как будто isEqualToString:@"true" является ложным, и выполняет второе условие, когда он явно записывается как истинный ...

Есть мысли?

1 Ответ

1 голос
/ 18 ноября 2009

Это сухой код (нуждается в тестировании), но я думаю, что я реализовал бы его примерно так. Надеюсь, это поможет.

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

Альтернативная реализация, которую вы можете найти предпочтительной:

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...