UITableView核心数据分组和关系

Bra*_*don 5 xcode core-data objective-c uitableview ios

我正在为我的第一个iPhone应用程序寻求帮助.到目前为止,核心数据的整个概念非常庞大.


核心数据模型:

核心数据模型

表格视图代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    //return 1;
    return [self.fetchedCoursesArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.

    return [self.fetchedRoundsArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"RoundsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:MyIdentifier];
    }

    Rounds * rounds = [self.fetchedRoundsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"Date: %@",rounds.date];
    //cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@",courses.city,courses.state];
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    Courses * courses = [self.fetchedCoursesArray objectAtIndex:section];
    return courses.name;
}
Run Code Online (Sandbox Code Playgroud)


表格图片(代码结果):

表视图

问题...

问题是分组不正确..它为每个组显示相同的两个项目.这些回合需要根据每个课程名称进行分组,但我不知道如何在代码中完成此任务,所以我求助于求助.我现在已经完成了数小时的研究..很多Stack Overflow问题和不同的教程都无济于事.

有人能为我提供一些方向吗?

更新

以下是数组在加载到视图中时的一些日志:

2013-10-23 23:27:16.771 Test 2[1566:70b] COURSES: (null)
2013-10-23 23:27:16.772 Test 2[1566:70b] ROUNDS: (null)
2013-10-23 23:27:16.773 Test 2[1566:70b] COURSES: (null)
2013-10-23 23:27:16.773 Test 2[1566:70b] ROUNDS: (null)
2013-10-23 23:27:16.775 Test 2[1566:70b] COURSES: (
    "<Courses: 0x8ab99a0> (entity: Courses; id: 0x8ab8b40 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Courses/p1> ; data: {\n    city = Arlington;\n    holes = 18;\n    name = \"Jones Park\";\n    rounds = \"<relationship fault: 0x8c35a30 'rounds'>\";\n    state = TX;\n})",
    "<Courses: 0x8ab9c30> (entity: Courses; id: 0x8ab8b50 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Courses/p2> ; data: {\n    city = Arlington;\n    holes = 22;\n    name = \"Test Park\";\n    rounds = \"<relationship fault: 0x8c35b40 'rounds'>\";\n    state = TX;\n})"
)
2013-10-23 23:27:16.776 Test 2[1566:70b] ROUNDS: (
    "<Rounds: 0x8c352a0> (entity: Rounds; id: 0x8c345f0 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Rounds/p1> ; data: <fault>)",
    "<Rounds: 0x8c354c0> (entity: Rounds; id: 0x8c34600 <x-coredata://5B7FCAFF-4AFF-4DB9-AAE1-93F0B8D920E2/Rounds/p2> ; data: <fault>)"
)
Run Code Online (Sandbox Code Playgroud)

我看到有一个关系问题..但我不认为有必要以我需要的方式将他们分组?也许它会让它变得更容易.希望这可以帮助..

squ*_*rog 2

无论该部分是第一门课程还是第二门课程,您似乎都使用相同的结果。您可能需要更好地钻取数据。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"RoundsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:MyIdentifier];
    }

    Courses * courses = [self.fetchedCoursesArray objectAtIndex:section];
    Rounds * rounds = [[courses.rounds allObjects] objectAtIndex:indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"Date: %@",rounds.date];
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

并且您也需要执行类似的操作numberOfRowsInSection- 只是为了确保返回正确的行数。它可能也值得研究,NSFetchedResultsController因为它可以消除处理UITableView.