伙计们,我遇到了一个我一直无法弄清楚的问题。我正在使用 UITableView 来显示我从已转换为字典的 JSON 对象收集的数据。
{
"start_from": "2014-08-10",
"list_tags": {
"2014-08-12": {
"0": [
{
"id": "5",
"tag": "555"
}
],
"1": [
{
"id": "5",
"tag": "556"
}
],
"-1": [
{
"id": "5",
"tag": "557"
}
]
},
"2014-08-10": {
"0": [
{
"id": "5",
"tag": "558"
},
{
"id": "5",
"tag": "565"
},
{
"id": "5",
"tag": "566"
}
],
"1": [
{
"id": "5",
"tag": "559"
}
],
"-1": [
{
"id": "5",
"tag": "560"
}
]
},
"2014-08-14": {
"0": [
{
"id": "5",
"tag": "561"
}
],
"1": [
{
"id": "5",
"tag": "562"
},
{
"id": "5",
"tag": "564"
}
],
"-1": [
{
"id": "5",
"tag": "563"
}
]
}
}
}
如果您查看 json 结构,它遵循如下内容
字典 -> list_tags -> { 特定日期(多个日期之一) -> { 特定数组(三个标识符值 -1,0,1 之一) -> { 特定对象(可以是多个)}}
我正在使用一个表格 View ,将格式化的日期作为标题。这里的挑战是在相同的标题下显示数组 (-1,0,1) 内的所有对象。由于每个数组中都可以有多个对象,因此要找出一个日期下存在多少个对象很复杂,因此,一个标题。
使用枚举,我获得了特定日期的对象数。
所以我的 titleForHeader 使用
self.listTagsKeys[section];
返回特定日期所有数组下的对象数。
我的节数是
[self.listTags count];
我使用映射到 [Date : Number Of Rows] 的字典来计算行数
[self.tagsForDates valueForKey:[self.listTagsKeys objectAtIndex:section]];
我在这里面临的挑战是如何在同一标题下显示 tableview 单元格的数据,因为我无法直接访问数据。
如果我做错了,请告诉我,以免我走错方向。 如果有人能告诉我如何在这种情况下使用迭代器,而不是简单地指出可以使用迭代器解决这个问题,或者帮助我想办法做到这一点,或者更好地组织数据,我将非常感激来自 JSON 结构。
如果这太令人困惑,请告诉我,这样我就能更好地解释它。
干杯!
部分将是日期,行将是该部分下每个日期的 (-1,0,1) 中的所有对象。由于这些对象中的每一个都有一个唯一标识符作为父对象,即 -1、0、1,我不确定我如何能够分辨出哪些行具有特定标识符。
因此 2014-08-10 将有 5 行,但使用三张图像中的一张(一张代表 -1,一张代表 0,一张代表 1)作为其 ImageView 。为了能够在特定行的 ImageView 中显示此图像,我需要知道它是什么类型。
最佳答案
我会首先创建一个对象来存储节和行对象。像这样的一个简单的 NSObject 子类 -
MySectionObject.h:
@class MySectionObject;
@interface MySectionObject: NSObject {
@property (strong, nonatomic) NSDate *date;
@property (readonly,nonatomic) NSArray *rowData;
-(id)initWithDate:(NSDate)date;
-(void)addRow:(MyRowObject *)row;
@end
我的SectionObject.m:
@interface MySectionObject: ()
@property (strong,nonatomic) NSMutableArray *rows;
@end
@implementation MySectionObject
-(id)initWithDate:(NSDate *)date
{
if (self=[super init]) {
_date=date;
_rows=[NSMutableArray new];
}
return self;
}
-(void)addRow(MyRowObject *)row
{
[self.rows addObject:row];
}
-(NSArray *)rowData {
{
return [NSArray arrayWithArray:self.rows];
}
@end
MyRowObject.h:
@interface MyRowObject: NSObject {
@property (strong, nonatomic) NSString *id;
@property (strong, nonatomic) NSString *tag;
@property NSInteger imageType
-(id)initWithType:(NSInteger)type id:(NSString *)id tag:(NSString *)tag;
@end
我的行对象.m:
@implementation MyRowObject
-(id)initWithType:(NSInteger)type id:(NSString *)id tag:(NSString *)tag
{
if (self=[super init]) {
_imageType=type;
_id=id;
_tag=tag;
}
return self;
}
@end
我对这两个属性使用了字符串,因为我不确定它们是数字还是只是您的示例数据。
然后您可以简单地枚举您的 JSON 以生成嵌套数组 -
self.tableData=[NSMutableArray new]; // This is a property that stores the data for your tableview
NSDictionary *listTags = [myJson objectForKey:@"list_tags"];
for (NSString *date in listTags) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *sectionDate = [dateFormatter dateFromString:date];
MySectionObject *section=[MySectionObject alloc]initWithDate:sectionDate];
NSDictionary *listTagDict = (NSDictionary *)[listTags objectForKey:date];
for (NSString *imageType in listTagDict) {
NSInteger *type=[imageTypes integerValue];
NSArray *rows=[listTagDict objectForKey:imageType];
for (NSDictionary *rowDict in rows) {
NSString *id=(NSString *)[rowDict objectForKey:@"id"];
NSString *tag=(NSString *)[rowDict objectForKey:@"tag"];
MyRowObject *row=[MyRowObject alloc] initWithType:type id:id tag:tag]];
[section addRow:row];
}
}
[self.tableData addObject:section];
}
现在部分的数量由 self.tableData.count
给出,部分中每行的数量由 [self.tableData objectAtIndex:sectionNumber].rowData.count 给出
您可以使用 MySectionObject *s=[self.tableData objectAtIndex:sectionNumber]
获取节标题的数据,并使用
MySectionObject *s=[self.tableData objectAtIndex:indexPath.section];
MyRowObject *r=[s.rowData objectAtIndex:indexPath.row];
关于ios - 从深层 JSON 结构构建 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25276365/