【发布时间】:2016-07-24 02:47:48
【问题描述】:
我在使用 tableView、CoreData 和 JSON 时遇到问题。而且我不知道该怎么办了。我正在尝试这样做:
This is what I need to do 问题在于我的 tableview 只是显示第一个对象,我需要显示所有。 json是:
{
"status":"OK",
"groups":[
{
"group_name":"Group 1",
"group_id":1,
"group_status":"FINISHED",
"group_is_active":"YES",
"group_students":[
{
"student_name":"Student 1",
"student_id":1,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 1"
},
{
"student_name":"Student 2",
"student_id":2,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 2"
},
{
"student_name":"Student 3",
"student_id":3,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 3"
},
{
"student_name":"Student 4",
"student_id":4,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 4"
},
{
"student_name":"Student 5",
"student_id":5,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 5"
}
]
},
{
"group_name":"Group 2",
"group_id":2,
"group_status":"NO_MESSAGES",
"group_is_active":"YES",
"group_students":[
{
"student_name":"Student 6",
"student_id":6,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 6"
},
{
"student_name":"Student 7",
"student_id":7,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 7"
},
{
"student_name":"Student 8",
"student_id":8,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 8"
},
{
"student_name":"Student 9",
"student_id":9,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 9"
},
{
"student_name":"Student 10",
"student_id":10,
"student_photo":"http:\/\/placehold.it\/120x120&text=Student 10"
}
]
},
...
我在 ViewController 上的方法代码是这样的:
- (void)fetchGroupsFromContext {
//Fetch and order alphabetical array
//SERVER
NSManagedObjectContext *contextServer = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSFetchRequest *fetchRequestServer = [NSFetchRequest fetchRequestWithEntityName:@"Server"];
NSSortDescriptor *descriptorServer = [[NSSortDescriptor alloc]initWithKey:@"status"
ascending:YES
selector:@selector(localizedStandardCompare:)];
fetchRequestServer.sortDescriptors = @[descriptorServer];
NSError *errorServer = nil;
NSArray *fetchedObjectsServer = [contextServer executeFetchRequest:fetchRequestServer
error:&errorServer];
Server *serverList = [fetchedObjectsServer firstObject];
self.server = [serverList.groups allObjects];
//GROUPS
NSManagedObjectContext *contextGroups = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
NSFetchRequest *fetchRequestGroups = [NSFetchRequest fetchRequestWithEntityName:@"Groups"];
NSSortDescriptor *descriptorGroups = [[NSSortDescriptor alloc]initWithKey:@"group_name"
ascending:YES
selector:@selector(localizedStandardCompare:)];
fetchRequestGroups.sortDescriptors = @[descriptorGroups];
NSError *errorGroups = nil;
self.fetchedObjectsGroups = [contextGroups executeFetchRequest:fetchRequestGroups
error:&errorGroups];
Groups *groupList = [self.fetchedObjectsGroups firstObject];
self.groups = [groupList.students allObjects];
NSLog(@"%@", self.groups);
[self.tableView reloadData];
}
我不知道该怎么办。我搜索了很多试图做到这一点。我不知道数组是否是最好的选择。但我需要一些帮助。
更新:
这是numberOfSectionsInTableView 和numberOfRowsInSection 上的内容:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.server count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.groups count];
}
当我打印 self.groups 时,它会告诉我这个:
(
"<Students: 0x7f98e8cc5bc0> (entity: Students; id: 0xd000000006480004 <x-coredata://191D8908-AAA2-4527-9D05-DB582D87570D/Students/p402> ; data: <fault>)",
"<Students: 0x7f98e8cf22f0> (entity: Students; id: 0xd000000003a80004 <x-coredata://191D8908-AAA2-4527-9D05-DB582D87570D/Students/p234> ; data: <fault>)",
"<Students: 0x7f98e8cb10a0> (entity: Students; id: 0xd000000002500004 <x-coredata://191D8908-AAA2-4527-9D05-DB582D87570D/Students/p148> ; data: <fault>)",
"<Students: 0x7f98e8cf1150> (entity: Students; id: 0xd000000006880004 <x-coredata://191D8908-AAA2-4527-9D05-DB582D87570D/Students/p418> ; data: <fault>)",
"<Students: 0x7f98e8cb7410> (entity: Students; id: 0xd000000002d00004 <x-coredata://191D8908-AAA2-4527-9D05-DB582D87570D/Students/p180> ; data: <fault>)"
)
更新:viewForHeaderInSectioncellForRowAtIndexPath
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//Fetch all students objects
Groups *groupList = [self.fetchedObjectsGroups objectAtIndex:section];
self.groups = [groupList.students allObjects];
// NSLog(@"%@", self.groups);
//Order array alphabetical
NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"group_name"
ascending:YES
selector:@selector(localizedStandardCompare:)];
self.server = [self.server sortedArrayUsingDescriptors:@[sort]];
HeaderTableViewCell* headerCell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCell"];
Groups *groupsList = self.server[section];
headerCell.titleHeader.text = groupsList.group_name;
return headerCell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Order array alphabetical
NSSortDescriptor *sort = [[NSSortDescriptor alloc]initWithKey:@"student_name"
ascending:YES
selector:@selector(localizedStandardCompare:)];
self.groups = [self.groups sortedArrayUsingDescriptors:@[sort]];
CellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Students *studentsList = self.groups[indexPath.row];
cell.titleCell.text = studentsList.student_name;
return cell;
}
【问题讨论】:
-
打印出来的
self.groups是什么? -
另外,您能否展示一下您对
numberOfRowsInSection和numberOfSectionsInTableView的看法? -
更新为
numberOfRowsInSection和numberOfSectionsInTableView和NSLog@Mike -
我还没有直观地解析所有内容,但我猜您想在
numberOfRowsInSection中执行self.groups[section].count之类的操作,或者类似的操作?每个部分都包含一个组对象,并且您希望该部分中的行数是group_students数组中的对象数,而不是组数,对吧?基本上,获取该部分的组,然后从该组中获取group_students,并将该数组用作该部分的计数。编辑:我有一种感觉,我不是 100% 理解你是如何构建这个的,但我认为这个想法很可能成立。) -
实际上@Mike 计数没问题,但问题在于
self.groups数组没有从其他组中获取其他学生,它只是在 JSON 上获取第一组学生。
标签: ios objective-c json uitableview core-data