我正在使用NSFetchedResultsController將數據填充到UITableView。 這是一個簡單的聊天應用程序,我想先載入最新的25條消息到桌子上,然後加載更多的用戶滾動查看舊信息(聊天信息按升序排列)。爲NSFetchedResultsController動態設置fetchLimit
我打電話,這將setFetchLimit的方法:對於NSFetchedResultsController在willDisplayCell:像這樣....
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
[self performSelector:@selector(getMoreMessages) withObject:nil afterDelay:1.0];
}
}
時已經顯示的UITableView的第一行,getMoreMessages將嘗試重置fetchLimit像這樣重新加載UITableView .....
- (void)getMoreMessages
{
maxListItems += 25;
NSLog(@"set maxListItems: %d", maxListItems);
[self.resultsController.fetchRequest setFetchLimit:maxListItems];
[self._tableView reloadData];
}
但是,它似乎沒有工作,表數據不會改變。 初始NSFetchRequest設置是這樣的...
NSFetchRequest *chatDataRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ChatData" inManagedObjectContext:appDelegate.managedObjectContext];
[chatDataRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(key != 0 OR messageNo != 0) and matchNo = %d", matchNo];
[chatDataRequest setPredicate:predicate];
NSSortDescriptor *sortDescripter1 = [[NSSortDescriptor alloc] initWithKey:@"status" ascending:YES];
NSSortDescriptor *sortDescripter2 = [[NSSortDescriptor alloc] initWithKey:@"messageNo" ascending:YES];
NSArray *sortDescripters = [[NSArray alloc] initWithObjects:sortDescripter1, sortDescripter2, nil];
[chatDataRequest setSortDescriptors:sortDescripters];
[sortDescripters release];
[sortDescripter1 release];
[sortDescripter2 release];
[chatDataRequest setFetchLimit:25];
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:chatDataRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:[NSString stringWithFormat:@"%d_chat.cache", matchNumber]];
[chatDataRequest release];
fetchedResultsController.delegate = self;
NSError *error;
BOOL success = [fetchedResultsController performFetch:&error];
if(!success) NSLog(@"error: %@", error);
self.resultsController = fetchedResultsController;
並回到剛纔的問題。
如何動態更改NSFetchedResultsController的fetchLimit?
任何命中將是真棒! 謝謝!
另外,請確保您重新加載UITableView後綴 –
它與uifetchedresultscontrollerdelegate控制器配合使用:didChangeObject:atIndexPath:forChangeType:newIndexPath:?不適合我=( – Irina
你需要調用[NSFetchedResultsController deleteCacheWithName:@「cacheName」]在執行此操作之前,Apple文檔NSFetchedResultsController.fetchRequest指出:「您不得更改它,它的謂詞或其排序描述符初始化後不禁用緩存或調用+ deleteCacheWithName「 –