【问题标题】:Tableview, core data and JSON in Objective CObjective C 中的 Tableview、核心数据和 JSON
【发布时间】:2016-07-24 02:47:48
【问题描述】:

我在使用 tableView、CoreData 和 JSON 时遇到问题。而且我不知道该怎么办了。我正在尝试这样做:

This is what I'm trying to do

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

我不知道该怎么办。我搜索了很多试图做到这一点。我不知道数组是否是最好的选择。但我需要一些帮助。

更新: 这是numberOfSectionsInTableViewnumberOfRowsInSection 上的内容:

- (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是什么?
  • 另外,您能否展示一下您对numberOfRowsInSectionnumberOfSectionsInTableView 的看法?
  • 更新为 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


【解决方案1】:

正如我们在讨论中提到的,您的查询和数据结构似乎比它们需要的要复杂一些。我会像这样构造它:

self.groups 将是一个 Group 对象数组,每个组对象都有一个 name NSString 属性和一个 members NSArray 属性。您需要做的就是获取和排序组,然后您的表格视图代码可能是这样的:

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

【讨论】:

  • 当我将Group 放入viewForHeaderInSection 时它起作用了,因为我使用的是自定义部分标题。但现在前 3 个部分显示第一个对象和其他对象并显示正常。
  • 您不应该在这些方法中对数组进行排序,这些方法会为每个单元格调用多次...
  • 感谢您的帮助。我删除了这些方法的排序,并解决了Group 部分的问题,将其放在cellForRowAtIndexPathindexPath.section 上它们工作得很好。
猜你喜欢
  • 2015-07-13
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
相关资源
最近更新 更多