2016-07-24 6 views
0

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'm trying to do

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

Was ist 'self.groups', wenn Sie es ausdrucken? – Mike

+0

Könnten Sie auch zeigen, was Sie für "numberOfRowsInSection" und "numberOfSectionsInTableView" haben? – Mike

+0

Aktualisiert mit 'numberOfRowsInSection' und 'numberOfSectionsInTableView' und 'NSLog' @Mike –

Antwort

0

Wie in unserer Diskussion erwähnt, scheint Ihre Abfrage und Datenstruktur etwas komplizierter zu sein, als sie sein müssen. Ich würde es so etwas wie diese Struktur:

self.groups würde eine Reihe von Group Objekten sein, und jede Gruppe Objekt verfügt über eine nameNSString Eigenschaft und eine membersNSArray Eigenschaft. Alles, was Sie tun müssen, ist, die Gruppen abzurufen und zu sortieren, und dann könnte Ihr Tabellenansichtscode ungefähr so ​​aussehen:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return self.groups.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    Group *group = self.groups[section]; 
    return group.members.count; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    Group *group = self.groups[section]; 
    return group.name; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // create your cell 
    GroupCell *cell = ..... 

    // Set this group on the cell, allowing it to update labels, etc. 
    Group *group = self.groups[indexPath.row]; 
    cell.group = group; 

    return cell; 
} 
+0

Es funktionierte, als ich die' Gruppe' in 'viewForHeaderInSection' platzierte, weil ich einen benutzerdefinierten Abschnitttitel verwende. Aber jetzt zeigen die ersten 3 Abschnitte das erste Objekt und die anderen und zeigen normal. –

+0

Kannst du cellForRowAtIndexPath anzeigen – Mike

+0

Aktualisiert da oben –

Verwandte Themen