有問題的應用程序有能力讓用戶將項目標記爲收藏夾。當用戶沒有保存收藏夾時,我想通知他們這個事實(主要是我討厭空白tableView的想法)。當沒有數據可用於tableView時顯示自定義標籤
我的numberOfRowsInSection
在沒有用戶標記爲收藏的項目時爲零。我想設置cell.textLabel.text = @「你沒有收藏」,但是當沒有項目的表cellForRowAtIndexPath
沒有被調用。
我可以測試numberOfRowsInSection
當遇到0時給出結果,然後在cellForRowAtIndexPath
中測試1行然後插入自定義文本,但是如果它們只有一個最喜歡的呢?
UPDATE
我試圖執行的想法我上面有,以下推薦的,但也許我不這樣做是正確的,可能是由於這樣的事實,這是一個fetchedResultsController與委託方法時更新表發生變化。
我得到這個錯誤,當我刪除小區時,有在表中,但一個小區:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1447.6.4/UITableView.m:976 Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted). with userInfo (null)
,並在沒有顯示在它崩潰首位細胞:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0)'
下面是相關代碼:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
if ([sectionInfo numberOfObjects]==0) {
return 1;
} else {
return [sectionInfo numberOfObjects];
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
if ([[_fetchedResultsController sections] objectAtIndex:0]==0) {
cell.textLabel.text = @"You have no favorites";
} else {
Shows *show = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = show.ShowsToSerials.serialName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", show.showTitle];
}
}
您是否願意爲您的解決方案1發佈代碼? – 2011-01-30 02:35:25
丹尼爾。再次感謝您的代碼。只是一個關於內存管理的問題。我在dealloc中釋放視圖(是最好的?),但我應該在哪裏發佈標籤? – 2011-06-09 01:25:48