2010-12-03 28 views
21

[tableView reloadData];を呼び出さずにテーブルビューのセクションヘッダー/フッターをリロードする方法はありますか?テーブルビュー全体をリロードせずにUITableViewのセクションヘッダー/フッタータイトルを変更する

実際、セクションのフッターのテーブルビューのセクションにセルの数を表示したいとします。テーブルビューは編集可能で、行を削除または挿入します。

– insertRowsAtIndexPaths:withRowAnimation: 
– deleteRowsAtIndexPaths:withRowAnimation: 

これらのメソッドはセクションフッタを更新しないようです。私は

- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section 

テーブルビューのデータソースメソッドこれらのメソッドを呼び出すときに不思議なこと、(2回!)と呼ばれているが、それは新しい値でテーブルビューを更新しません!

誰でもこの問題を解決する方法はありますか?

答えて

7

私は間接的にそれをやりました。私はUILabelを作成し、それをセクションヘッダー/フッターとして設定しました。

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    // update sectionFooterView.text  
    return sectionFooterView; 
} 

- (void)viewDidLoad { 
    // create sectionFooterView in Interface Builder, change the frame here 
    // use Font-size:15 , BG-Color: clearColor , text-Color: RGB=(97,105,118) 
    // and Text-alignment:Center to look like table view's original footer 
    sectionFooterView.frame = CGRectMake(0, 10, 320, 12); 
} 

カスタムフッタービューを設定せずにこれを行う方法を知っている人はいますか?

+0

でそれを行う方法です。 '[sectionFooterView setText:@" Some text "]'を呼び出すたびに、画面上で更新されます。 –

2

のUITableViewのマニュアルを確認するか、より具体的に -reloadSections:withRowAnimation:

+0

これは動作しますが、セクションヘッダー/フッターを更新するだけでなく、すべてのセルも更新するので、最も効率的ではありません。アリの答えが行く方法ですね。 –

+1

私は、あなたがフッターやヘッダーを変更すると内容も変わるとAppleが期待していると思います。 しかし、半静的に表示されるテキストだけでなく、viewForFooter/Headerを使用してカスタマイズする方が簡単です。 デフォルトのセクションのヘッダーとフッターは多くの書式設定を行い、このようなオーバーライドを簡単なものにするのが最適なオプションではないことに注意してください。 – Tim

32

あなただけの基本的なテキストヘッダーやフッターを持っている場合は、あなたがそれらを直接アクセスすることができます。

[tableView footerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForFooterInSection:indexPath.section]; 

を同様にヘッダー用:

[tableView headerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForHeaderInSection:indexPath.section]; 
+2

しかし、 'textLabel'に直接アクセスしても、新しいテキストに合うようにフレームは変更されません。フッター/ヘッダビューで 'sizeToFit'を呼び出さなければなりませんか? – Blip

+0

テーブルビューでレイアウトを再計算するには、beginUpdates/endUpdatesを呼び出す必要があります –

+0

金色! tableView.headerViewForSection(indexPath.section)?textLabel.text = tableView(tableView、titleForHeaderInSection:indexPath.section) – DogCoffee

3

これは別の方法です。

UITableViewHeaderFooterView *headerView=[self.tableView headerViewForSection:1]; 
CATransition *animation = [CATransition animation]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
animation.type = kCATransitionFade; 
animation.duration = 0.35; 
[headerView.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"]; 
headerView.textLabel.text=[self tableView:self.tableView titleForHeaderInSection:1]; 
[headerView.textLabel sizeToFit]; 
11

これは動作するはずです:

UIView.setAnimationsEnabled(false) 
    tableView.beginUpdates() 

    if let containerView = tableView.footerViewForSection(0) { 
     containerView.textLabel!.text = "New footer text!" 

     containerView.sizeToFit() 
    } 

    tableView.endUpdates() 
    UIView.setAnimationsEnabled(true) 
+0

iOS 9、Swift 2.2ではかなりうまく動作します。そして、フレームの四角形を取り込むよりはるかにクリーンで、確実に避けるべきです。 – maganap

1

これは、あなたがこれはそれを行う方法ですスウィフト3.0

tableView.beginUpdates() 
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text" 
tableView.endUpdates() 
関連する問題