問題のアプリには、ユーザーがアイテムをお気に入りとしてマークする機能があります。ユーザーがお気に入りを保存していない場合は、その事実を通知したいと思います (ほとんどの場合、空の tableView のアイデアは嫌いです)。
numberOfRowsInSection
ユーザーがお気に入りとしてマークした項目がない場合、Myはゼロです。cell.textLabel.text = @"お気に入りがありません" を設定したいのですが、テーブルのアイテムがない場合cellForRowAtIndexPath
は呼び出されません。
numberOfRowsInSection
0 に遭遇したときに結果を返すようにテストし、次に 1 行をテストしてcellForRowAtIndexPath
カスタム テキストを挿入することはできますが、お気に入りが 1 つしかない場合はどうでしょうか。
アップデート
上記のアイデアと以下で推奨するアイデアを実装しようとしましたが、変更が発生したときにテーブルを更新するデリゲート メソッドを備えた fetchedResultsController であるという事実のために、おそらく正しく実行していません。
テーブルにセルが 1 つしかないときにセルを削除すると、このエラーが発生します。
*** 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];
}
}