0

我有以下代码试图让 a 的一个部分UITableView链接到核心数据模型,而另一部分是一个带有文本的简单单元格:

- (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]

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

4

3 回答 3

1

如果您在 Core Data 中有 1 个部分,则不得在中要求第 1 部分(即第二部分) tableView:numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 1) {
// wrong:
//  id <NSFetchedResultsSectionInfo> sectionInfo = 
//                   [self.fetchedResultsController sections][section]; 
//                                                            ^^^^^^^ will be 1
// correct:
    if ([[self.fetchedResultsController sections] count] > 0) {
        id <NSFetchedResultsSectionInfo> sectionInfo = 
                         [self.fetchedResultsController sections][0];
        return [sectionInfo numberOfObjects];
    } 
    else {
        // core data is empty
        return 0;
    }
  } else {
// section 0
    return 1;
  }
 }

你应该检查你的 NSFetchedResultsController 中是否有一个部分。如果核心数据中没有对象,[0]也会导致越界异常。

由于您在第二部分中有核心数据结果,因此numberOfSectionsInTableView:如果您没有任何对象,则可以返回 1。然后你不必检查没有对象t:numberOfRowsInSection:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([[self.fetchedResultsController sections] count] > 0) {
        return 2;
    }
    else {
        // no objects in core data. only show first section
        return 1;
    }
}

当然,当您从 NSFetchedResultsController 请求某些内容时,您必须更改所有 indexPaths。控制器在第 1 节中没有任何对象!

// wrong:
// Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:indexPath];
// correct:
Subject *subject = (Subject *)[self.fetchedResultsController objectAtIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:0]];
于 2013-10-22T13:38:45.293 回答
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];
}
于 2013-10-22T13:45:12.557 回答
0

您的数组超出范围,请使用以下代码

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  if (section == 1) {

    return [self.fetchedResultsController count];
  } else {
    return 1;
  }
 }
于 2013-10-22T13:37:14.573 回答