【问题标题】:UITableView Core Data Grouping and RelationshipsUITableView 核心数据分组和关系
【发布时间】:2013-11-02 12:02:15
【问题描述】:

我正在为我的第一个 iPhone 应用寻求帮助。到目前为止,Core Data 的整个概念非常庞大。


核心数据模型:

表格查看代码:

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


表格视图图片(代码结果):


问题...

问题是分组不正确。它为每个组显示相同的两个项目。这些回合需要根据每个课程名称进行分组,但我不知道如何在代码中完成此操作,所以我求助于寻求帮助。我现在已经进行了数小时的研究.. 很多 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>)"
)

我发现存在关系问题..但我认为不需要关系来按照我需要的方式对它们进行分组?也许它会更容易。希望这会有所帮助..

【问题讨论】:

    标签: ios objective-c xcode uitableview core-data


    【解决方案1】:

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

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

    您还需要为numberOfRowsInSection 做类似的事情——只是为了确保您返回正确的行数。 NSFetchedResultsController 可能也值得研究一下,因为在处理 UITableView 中的核心数据时会让人头疼。

    【讨论】:

    • 感谢您的帮助!这就是我使用您的代码得到的结果(我将 objectAtIndex:section 修改为 objectAtIndex:indexPath.sectioncl.ly/S8AZ 这就是我创建这些数组的方式:self.fetchedRoundsArray = [appDelegate getAllRounds]; self.fetchedCoursesArray = [appDelegate getAllCourses]; cl.ly/S890
    • 好的,courses.rounds 的日志输出是什么?试试Rounds * rounds = [courses.rounds objectAtIndex:indexPath.row];
    • Property rounds not found on object of type Courses.
    • 好的,你的课程模型和轮次之间的关系是什么?
    • 嗯,我不知道原始问题中的第一张图片是否有帮助,但这是另一张.. cl.ly/S7kX
    【解决方案2】:

    因为你在 numberOfRowsInSection 方法中返回 [self.fetchedRoundsArray count], 当然,您必须返回关联的 RoundsArray。

    请添加日志以打印两个获取的数组。

    【讨论】:

    • 我为你添加了日志。我真的不知道将日志代码放在哪里,所以我在 numberOfSectionsInTableView 方法中添加了两个 NSLog。我认为至少看看数组包含什么就足够了。让我知道你的想法。在旁注中,如果您认为我的问题值得提出,您会愿意支持我的问题吗?我真的很想达到 10,这样我也可以发布图片!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多