HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UITableView核心数据分组和关系大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在为我的第一个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;
}

表格图片(代码结果):

问题…

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

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

解决方法

无论该部分是第一道菜还是第二道菜,看起来您使用相同的结果.您可能需要更好地深入挖掘数据.
- (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中处理Core Data需要很多麻烦.

大佬总结

以上是大佬教程为你收集整理的ios – UITableView核心数据分组和关系全部内容,希望文章能够帮你解决ios – UITableView核心数据分组和关系所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。