我在我的应用程序中使用了一个UITableViewController表,并且我添加了一个NSFetchedResultsController来提供表中显示的数据(设置为自己的委托).
然而,我想添加一个唯一的单元格作为表的最后一个单元格,与NSFetchedResultsController的谓词生成的项目无关,我希望这个单元格始终位于表的底部.
- - (NSUInteger)numberOfSectionsInTableView:(UITableView *)sender
- {
- return [[self.fetchedResultsController sections] count] + 1;
- }
- - (NSUInteger)tableView:(UITableView *)sender numberOfRowsInSection:(NSUInteger)section
- {
- return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects] + 1;
- }
然后捕获这样的额外行的情况,如此生成(表只有1个部分):
- - (UITableViewCell *)tableView:(UITableView *)sender
- cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.row == [self.fetchedResultsController.fetchedObjects count]) {
- //Generate the last cell in table.
- } else {
- //Generate the rest of the cells like normal.
- }
- return nil; //To keep the compiler happy.
- }
这将检查索引是否是最后一个单元格,并适当地处理.
但是我仍然在运行时收到以下错误:
- *** Terminating app due to uncaught exception 'NSRangeException',reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
任何想法是什么导致这个?还是有更好的方法来添加一个额外的行到由NSFetchedResultsController控制的表视图?