【发布时间】:2012-03-25 03:41:39
【问题描述】:
我在我的应用程序中使用UITableViewController 作为表格,并添加了NSFetchedResultsController 以提供要在表格中显示的数据(将self 设置为代表)。
但是我想添加一个唯一的单元格作为表格的最后一个单元格,与NSFetchedResultsController 的谓词产生的项目无关,我希望这个单元格始终位于表格的底部。
我已经尝试在表格视图数据源中简单地为这些方法添加 1:
- (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 控制的表视图添加额外的行?
【问题讨论】:
标签: ios ios5 uitableview nsfetchedresultscontroller