2010-12-03 49 views
21

有沒有任何方法可以在不調用 [tableView reloadData];的情況下重新加載表格視圖的分節頁眉/頁腳?在不重新載入整個表格視圖的情況下更改UITableView的分節頁眉/頁腳標題

實際上,我想在其分節頁腳的表格視圖部分中顯示單元格的數量。表視圖是可編輯的,我刪除或插入行使用

– insertRowsAtIndexPaths:withRowAnimation: 
– deleteRowsAtIndexPaths:withRowAnimation: 

看來,這些方法不會更新節頁腳。奇怪的是,當我把這些方法表視圖數據源方法

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

被稱爲(兩次!),但它不更新使用新值表視圖!

任何人有任何想法如何解決這個問題?

回答

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); 
} 

有沒有人知道沒有設置自定義頁腳視圖的方法嗎?同樣

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

頁眉:

+0

這是做它的方式。每當你調用`[sectionFooterView setText:@「一些文本」]`它會在屏幕上更新。 – 2013-07-18 15:03:44

2

檢查文檔的UITableView,或者更具體地說 -reloadSections:withRowAnimation:

+0

這可以工作,但它不是最有效的,因爲它不會更新節頭/頁腳,而且還會更新所有單元格。似乎阿里的答案是要走的路。 – 2012-01-31 08:24:43

+1

我認爲,蘋果預計,如果你改變頁腳或頁眉,內容也會改變。 但是,是的,如果你想定製的不僅僅是半靜態文本顯示,然後使用viewForFooter/Header可以更容易。 請注意,默認的頁眉和頁腳會爲您做很多格式設置,並且像這樣覆蓋它們以獲取某些細節可能也不是最佳選擇。 – Tim 2012-02-02 22:01:27

32

如果你只是有一個基本的文本頁眉或頁腳,您可以直接訪問它們

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

但直接訪問`textLabel`不會改變框架以適應新的文本。我必須在頁腳/標題視圖中調用`sizeToFit`嗎? – Blip 2015-05-27 00:03:17

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) 
1

這是你怎麼做的雨燕3.0

tableView.beginUpdates() 
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text" 
tableView.endUpdates()