2009-11-17 49 views
0

我有一个字典对象的数组,我试图做一个简单的比较字典对象内的记录的内容。这里是我的代码字符串比较困境

- (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"是假的,做第二个条件,当它显然是被记录为真...

有什么想法?

+0

是U确保其“真”,而不是像“真实”也许? – Daniel 2009-11-17 20:46:21

+0

另外:为什么你要存储布尔值的字符串表示?为什么不存储一个' - [NSNumber numberWithBool:]'作为'@“可用”'的字典值? – 2009-11-17 20:50:29

+1

也可以,''[isAvailable boolValue]'而不是等于true的测试会更容易。肯定没有理由在BOOL上使用开关而不是if/else;你的问题在别的地方 – newacct 2009-11-17 20:54:17

回答

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