私が見つけた最善の解決策は、セルの代わりにタイトルを作成して、削除と更新(コアデータ)の問題を回避することです。行やセクションがない場合、私が知っているタイトルは表示されません。だから私はこの2つの方法でそれを解決します:
-(void) hideTitle{
self.tableView.tableHeaderView = nil;
}
-(void) showTitle{
self.contener = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 40)];
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
self.label.text = NSLocalizedString(@"Header for the table", @"");
self.label.shadowOffset = CGSizeMake(0, 1);
self.label.font = [UIFont boldSystemFontOfSize:22];
self.label.backgroundColor = [UIColor redColor];
[self.contener addSubview:[self label]];
self.tableView.tableHeaderView = self.contener;
}
show という名前のこれをnum_of_sections..に置きました。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([[[self fetchResultsController] fetchedObjects] count] == 0)
[self showTitle]; .....}
このメソッドは、行を削除するときにうまく機能します。新しい行を作成するときに使用する hide という名前の 2 番目のメソッド (メソッドを「保存メソッド」に入れます) このメソッドはタイトルを削除します。
編集:
別の方法:
行番号の前に試してみると...
if([[bla bla] count] == 0 && is_deleted == 0)
return 1;
セルも変更:
if([[bla bla] count] == 0){
[[cell textLabel]setText:@"no results"];
[[cell detailTextLabel] setText:nil];
cell.accessoryType = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
}else{
cell.userInteractionEnabled = YES;
cell.accessoryType = UITableViewCellSelectionStyleBlue;
... }
問題は削除行であるため、このセルの削除オプションをキャンセルすることを忘れないdidLoad method bool = 0;
場合は、bool を使用してクラッシュを回避します!!delete I insert 1 to the bool
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete && [[bla bla] count] != 0)
{ ... }
このソリューションの問題は、完璧ではないということです。最後の行を削除すると、テーブルを再度ロードしたときにのみtableViewが空になり、タイトルが必要なものではないように見えます。
誰かがよりエレガントな解決策を持っている場合は、あなたの答えを正しい答えとしてチェックすることを約束してください。