Ich habe ein Problem mit einem TableView, CoreData und JSON. Und ich weiß nicht mehr, was ich tun soll. Ich versuche, dies zu tun:Tabellenansicht, Kerndaten und JSON in Objective C
This is what I need to do Das Problem ist in meinem Tableview nur ist das erste Objekt zeigt, und ich brauche alle zu zeigen. Die json ist:
{
"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"
}
]
},
...
Und mein Code auf Verfahren auf Viewcontroller ist dies:
- (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];
}
Ich weiß nicht, was zu tun ist. Ich habe zu viel gesucht, um das zu versuchen. Ich weiß nicht, ob ein Array die beste Option dafür ist. Aber ich brauche Hilfe.
UPDATE: Hier ist, was auf numberOfSectionsInTableView
und numberOfRowsInSection
:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.server count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.groups count];
}
Wenn ich self.groups
drucken Sie es mir zeigen dies:
(
"<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>)"
)
UPDATE: Die viewForHeaderInSection
cellForRowAtIndexPath
#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;
}
Was ist 'self.groups', wenn Sie es ausdrucken? – Mike
Könnten Sie auch zeigen, was Sie für "numberOfRowsInSection" und "numberOfSectionsInTableView" haben? – Mike
Aktualisiert mit 'numberOfRowsInSection' und 'numberOfSectionsInTableView' und 'NSLog' @Mike –