【问题标题】:access BOOL value and set table cell from an array访问 BOOL 值并从数组中设置表格单元格
【发布时间】:2013-10-28 19:14:11
【问题描述】:

我有一个分段的 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];

我已经创建了表格视图并填充了所有内容,但我要做的是根据数组内的一些值设置文本颜色

{
        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 的,我如何使用我设置的 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;

}

我试图将支付的值设置为数组中的支付值。有什么想法吗?

【问题讨论】:

  • 您添加了很多代码,但我仍然不了解您的数据模型或您的问题究竟是什么。我建议您将问题精简到重点 - 我有这个数据,我怎么能得到这个 - 这就是我所拥有的,但它却变成了 XX。

标签: ios json uitableview nsarray


【解决方案1】:

如果您需要将原生数据类型存储在 int、double、bool 等数组中,您应该使用:[array addObject:[NSNumber numberWithBool:bool]];

然后,恢复类似对象并使用如下值:[[arr objectAtIndex:i] boolValue]

【讨论】:

    【解决方案2】:

    您说这些值存储在一个数组中,但您显示的是一个字典。只有对象可以在 NSArray 或 NSDictionary 中。布尔值通常存储为 NSNumber(因为 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 对此进行了介绍。

    【讨论】:

    • 这是我最初的做法。我上了一节课,一切都很好。我唯一的问题是当我尝试对表格进行分区时,我无法通过 watch 关键字填充这些部分?
    • 您可以根据需要对表格进行分区。这是另一个问题的主题。您可能想看看 TLIndexPathTools,它可以帮助您显着简化表数据建模:github.com/wtmoose/TLIndexPathTools
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 2016-09-01
    • 1970-01-01
    • 2013-03-17
    • 2023-04-03
    • 2012-02-19
    • 2023-03-04
    相关资源
    最近更新 更多