我有一个分区的UITableView,它填充了我数据库中的数据。数据采用JSON格式,然后通过人们的工作班次分成三个MutableArrays。
NSData *data = [NSData dataWithContentsOfURL:url];
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
ridersInVan = [[NSMutableArray alloc] init];
first = [[NSMutableArray alloc] init];
second = [[NSMutableArray alloc] init];
third = [[NSMutableArray alloc] init];
for (int i=0; i< [json count]; i++)
{
item = json[i];
if ([@"1" isEqual: item[@"watch"]] )
{
[first addObject:item];
} else if ([@"2" isEqual: item[@"watch"]] )
{
[second addObject:item];
} else if ([@"3" isEqual: item[@"watch"]] )
{
[third addObject:item];
}
}
ridersInVan = [NSMutableArray arrayWithObjects:first, second, third, nil];
我创建了tableview并填充了所有内容,但我要做的是根据数组中的一些值设置文本颜色
{
driver = 0;
expiration = "2013-10-08";
greenCard = 1;
id = 5;
name = "greg smith";
paid = 1;
phoneNumber = "123 345-1234";
showNumber = 1;
watch = 3;
}
司机,付费,显示都是BOOL我怎样才能通过使用bool值来设置textcolor
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[[ridersInVan objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"name"];
cell.detailTextLabel.text = [[[ridersInVan objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:@"phoneNumber"];
if (paid == NO && currentDate <= 8)
{
cell.textLabel.textColor = START;
} else if (paid == YES) {
cell.textLabel.textColor = PAID;
isPaid = YES;
} else if (paid == NO && currentDate > 8 && currentDate <= 15)
{
cell.textLabel.textColor = LATE;
isLate = YES;
} else if (paid == NO && currentDate > 15 && currentDate <= 28)
{
cell.textLabel.textColor = AFTER_VAN_DATE;
afterDate = YES;
} else if (paid == NO && currentDate > 28)
{
cell.textLabel.textColor = OFF_OF_VAN;
offOfVan = YES;
}
return cell;
}
我试图将付费值设置为数组中的付费值..有什么想法吗?
答案 0 :(得分:0)
如果需要在int,double,bool等数组中存储本机数据类型,则应使用:[array addObject:[NSNumber numberWithBool:bool]];
然后,像对象一样恢复并使用如下值:[[arr objectAtIndex:i] boolValue]
答案 1 :(得分:0)
您说这些值存储在数组中,但您显示的是字典。只有对象可以在NSArray或NSDictionary中。布尔值通常存储为NSNumbers(因为NSJSONSerialization将所有对象存储为NSString,NSNumber,NSArray,NSDictionary或NSNull)。
你需要做这样的事情:
NSNumber *paidNumber = [myDictionary objectForKey:@"paid"];
BOOL paid = [paidNumber boolValue];
挖掘数组和字典会很快变老。您可能应该创建新的类来管理这些数据,这样您就可以执行以下操作:
Car *car = [Cars carAtIndexPath:indexPath];
if (car.paid) {
// paid…
} else {
// not paid…
}
nice tutorial here中包含了这个内容。