2009-10-10 45 views
21

我現在面臨一個問題,我解決不了...... 我有一個分組表,它的部分頁眉和頁腳節得到正確顯示一經推出得益於更改UITableView節頭/頁腳WHILE運行應用程序?

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

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

然而,我不知道如何在稍後更新它們,我不知道如何更改這些文本,這是非常令人沮喪的,因爲在我看來,它不應該比改變文本標籤或其他任何東西更難......(某些「。文本「屬性...)

我搜索了所有通過文檔,沒有運氣...

任何幫助是高度&非常感謝! 問候, 恩里科

+0

有沒有一種方法來更新頁眉/頁腳無需重新加載整個表...或整個部分?更新*只是*頁眉/頁腳。 – Patricia 2010-10-17 15:28:42

+0

我不相信只更新tableview的一部分會讓你有所收穫,或者你會看到性能上的差異。 – 2010-10-17 15:42:48

回答

25

在委託方法,添加一個方法調用返回段的名稱,例如:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    switch (section) { 
     case firstSectionTag: 
      return [self firstSectionTitle]; 
     case secondSectionTag: 
      return [self secondSectionTitle]; 
     // ... 
     default: 
      return nil; 
    } 
} 

- (NSString *)firstSectionTitle { 
    // generate first section title programmatically, e.g. "return [[NSDate date] description];" 
} 

// ... 

然後,當你需要更新的章節標題,發送NSNotification觸發事像下面的方法:

- (void)refreshTableSectionTitles:(NSNotification *)notification { 
    [tableView reloadData]; 
} 

如果表很大,你想更好的控制,通過與包含要重新加載部分的NSDictionaryNSNotification,請閱讀-refreshTableSectionTitles中的字典以取回NSInteger部分,並使用表格視圖的-reloadSections:withRowAnimation:重新加載該特定部分。

7

您可以使用:

-(void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation 

而且在

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

定義語句新的標題文字。

這將在改變頁眉/頁腳的同時製作出不錯的動畫。

4

使用此行代碼要更改標題:

[[self theTableViewToChangeItsHeader]reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 
+1

在iOS 7上會導致該部分中的單元消失。 – malhal 2014-01-31 02:14:51

+0

這個問題與ios 5.1有關。你是否認真對待它? :D – 2014-02-01 16:38:54

-4

在你TableViewController使用

[self.tableView reloadSectionIndexTitles]; 
+0

不重新加載頁腳文本 – malhal 2013-12-10 19:42:16

+1

這隻適用於滾動時沿着屏幕右側顯示的索引欄。 – macserv 2014-05-13 21:32:21

相關問題