2009-11-17 46 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; 
}