我正在为我的第一个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需要很多麻烦.