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"
是假的,做第二个条件,当它显然是被记录为真...
有什么想法?
是U确保其“真”,而不是像“真实”也许? – Daniel 2009-11-17 20:46:21
另外:为什么你要存储布尔值的字符串表示?为什么不存储一个' - [NSNumber numberWithBool:]'作为'@“可用”'的字典值? – 2009-11-17 20:50:29
也可以,''[isAvailable boolValue]'而不是等于true的测试会更容易。肯定没有理由在BOOL上使用开关而不是if/else;你的问题在别的地方 – newacct 2009-11-17 20:54:17