字符串比较困境

时间:2009-11-17 20:42:00

标签: iphone objective-c

我有一个字典对象数组,我正在尝试对字典对象内部记录的内容进行简单比较。这是我的代码

- (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 个答案:

答案 0 :(得分:1)

这是干编码的(需要测试),但我想我会实现它类似于此。希望它有所帮助。

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