0

データベースからのデータが取り込まれたセクション化された UITableView があります。データは JSON 形式で、勤務シフトによって 3 つの MutableArray に分割されます。

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

Driver、payed、shownumber はすべて 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;

}

有料の値を配列内の有料の値に設定しようとしています..何かアイデアはありますか?

4

2 に答える 2

0

int、double、bool などの配列にネイティブ データ型を格納する必要がある場合は、次を使用する必要があります。[array addObject:[NSNumber numberWithBool:bool]];

そして、オブジェクトのように回復し、次のように値を使用します。[[arr objectAtIndex:i] boolValue]

于 2013-10-28T19:23:57.883 に答える
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…
}

これについては、こちらの素晴らしいチュートリアルで説明されています

于 2013-10-28T19:29:45.630 に答える