This question already has an answer here:
numberOfRowsInSection: method for core data and multiple sections
                                
                                    (1个答案)
                                
                        
                                5年前关闭。
            
                    
我有以下代码尝试将UITableView的一部分链接到Core Data Model,另一部分是带有文本的简单单元格:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 1) {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
  } else {
    return 1;
  }
 }

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

   if (indexPath.section == 0) {
    cell.subject.text = @"Favourites";
   } else {
    Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.subject.text = subject.subject;
    cell.subjectColour.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", subject.colour]];
   }

   return cell;
}


但是,应用程序崩溃了,我得到了以下日志输出:


  由于未捕获的异常'NSRangeException'而终止应用程序,原因:'*-[__ NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]


关于我在做什么错的任何想法?提前致谢。

最佳答案

这是正常的,如果使用NSFetchedResultsController而不指定sectionKeyName,则返回的一个部分的索引等于0。

在您的样本中,您假设:

区段索引0是您的收藏夹

if (indexPath.section == 0) {
    cell.subject.text = @"Favourites";
   }


而部分索引1是您的NSFecthedResultsController结果

else {
    Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.subject.text = subject.subject;
    cell.subjectColour.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", subject.colour]];
}


但是您必须重新创建IndexPath才能将其部分索引设置为0

尝试这个 :

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 1) {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][0];
    return [sectionInfo numberOfObjects];
  } else {
    return 1;
  }
 }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    ....

    else {
        Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:    [NSIndexPath indexPathForItem:indexPath.row inSection:0]
    ];
        cell.subject.text = subject.subject;
        cell.subjectColour.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", subject.colour]];
       }


对于didSelectRowAtIndexPath,您使用相同的模式:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 1) {
    Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:0]];
    [self showList:subject animated:YES];
}

关于ios - UITableView中的一节Core Data和另一节NSMutableArray ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19519334/

10-10 23:56