2013-10-28 66 views
0

我有一个分段的UITableView,其中填充了来自我的数据库的数据。数据采用JSON格式,然后通过一个人的工作变化分成三个MutableArrays。访问BOOL值并从数组中设置表格单元格

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

驱动程序,支付,shownumber是文本颜色所有BOOL的我怎样才能使用布尔值我有这件事

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

}

设置文本颜色210

即时尝试设置付款价值的阵列中的付费价值..任何想法?

+0

您已经添加了相当多的代码,但我仍然不明白您的数据模型或您的问题实际是什么。我建议你将问题简化为重点 - 我有这个数据,我怎么能得到这个数据 - 这是我的,但它取而代之。 – Wain

回答

0

如果您需要存储如int一个数组中的一个本地数据类型,双,布尔,你应该使用:[array addObject:[NSNumber numberWithBool:bool]];

,然后恢复状物品和使用这样的值:[[arr objectAtIndex:i] boolValue]

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

+0

这是我最初的做法。我上了一堂课,一切都很好。我唯一的问题是,当我试图对表格进行分区时,我无法使用watch关键字填充这些部分? –

+0

您可以根据需要对表格进行分区。但这是另一个问题的主题。您可能需要查看TLIndexPathTools,它可以帮助您显着简化表数据建模:https://github.com/wtmoose/TLIndexPathTools –

相关问题