2010-12-03 48 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()