我在我的應用中使用UITableViewController
作爲表格,並且我添加了NSFetchedResultsController
以提供要在表格中顯示的數據(設置爲self
,因爲它是委託)。向NSFetchedResultsController管理的UITableView添加額外的行
但是我想添加一個獨特的單元格作爲表格的最後一個單元格,與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編程還是很新的:-)) – 2012-03-07 15:46:09
是的,它變得非常黑客。這個想法非常簡單...非常感謝! – Shingoo 2012-04-04 19:24:02
但這看起來像它 - http://stackoverflow.com/questions/10965098/nsfetchedresultscontroller-prepend-a-row-or-section – 2014-01-31 23:12:55