更改UITableView的节头/页脚标题而不重新加载整个表视图

时间:2010-12-03 15:21:53

标签: ios uitableview header footer reload

有没有办法在不调用的情况下重新加载表视图的节头/页脚  [tableView reloadData];

实际上,我想在其章节页脚的表格视图的部分中显示单元格的数量。表视图是可编辑的,我使用

删除或插入行
– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:

似乎这些方法不会更新部分页脚。奇怪的是,当我调用这些方法时,表视图数据源方法

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

被调用(两次!)但它不会使用新值更新表视图!!

任何人都知道如何解决这个问题?

10 个答案:

答案 0 :(得分:34)

如果您只有基本文本页眉或页脚,则可以直接访问它们:

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

类似于标题:

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

答案 1 :(得分:16)

这应该有效:

    UIView.setAnimationsEnabled(false)
    tableView.beginUpdates()

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

        containerView.sizeToFit()
    }

    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)

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

有没有人知道如何在不设置自定义页脚视图的情况下执行此操作?

答案 3 :(得分: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];

答案 4 :(得分:3)

这是你在Swift 3.0中的方法

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

答案 5 :(得分:2)

查看UITableView的文档,或者更具体地说 -reloadSections:withRowAnimation:

答案 6 :(得分:1)

你也可以这样做

override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
    switch section {
        case 0:
            return "Some string"
        default:
            return ""
    }
}

答案 7 :(得分:1)

这是一个UITableView扩展,它是基于上面的一些答案(请注意,表头标题的人工大写)来刷新tableView页眉/页脚标题的便捷快捷方式。

extension UITableView {

    func refreshHeaderTitle(inSection section: Int) {
        UIView.setAnimationsEnabled(false)
        beginUpdates()

        let headerView = self.headerView(forSection: section)
        headerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForHeaderInSection: section)?.uppercased()
        headerView?.sizeToFit()

        endUpdates()
        UIView.setAnimationsEnabled(true)
    }

    func refreshFooterTitle(inSection section: Int) {
        UIView.setAnimationsEnabled(false)
        beginUpdates()

        let footerView = self.footerView(forSection: section)
        footerView?.textLabel?.text = self.dataSource?.tableView?(self, titleForFooterInSection: section)
        footerView?.sizeToFit()

        endUpdates()
        UIView.setAnimationsEnabled(true)
    }

    func refreshAllHeaderAndFooterTitles() {
        for section in 0..<self.numberOfSections {
            refreshHeaderTitle(inSection: section)
            refreshFooterTitle(inSection: section)
        }
    }
}

答案 8 :(得分:0)

我发现,如果页脚文本从填充一行更改为多行更改为一行,则页脚文本将无法正确更新。重置标签的宽度和高度将解决此问题。

    UIView.setAnimationsEnabled(false)
    textLabel.frame = CGRect(x: textLabel.frame.minX,
                             y: textLabel.frame.minY,
                             width: 0,
                             height: 0)
    tableView.beginUpdates()
    textLabel.text = "Your text"
    textLabel.sizeToFit()
    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)

答案 9 :(得分:0)

对我来说,只需在表格视图上用reloadSections重新加载该部分即可

相关问题