【问题标题】:Changing UITableView's section header/footer title without reloading the whole table view在不重新加载整个表格视图的情况下更改 UITableView 的部分页眉/页脚标题
【发布时间】:2011-05-19 19:37:02
【问题描述】:

有什么方法可以在不调用的情况下重新加载表格视图的部分页眉/页脚 [tableView reloadData];?

事实上,我想在其页脚中显示表格视图的部分中的单元格数。表格视图是可编辑的,我使用删除或插入行

– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:

似乎这些方法不会更新节页脚。奇怪的是,当我将这些方法称为表视图数据源方法时

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

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

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: ios uitableview header footer reload


    【解决方案1】:

    如果你只有一个基本的文本页眉或页脚,你可以直接访问它们:

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

    标题也类似:

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

    【讨论】:

    • 但直接访问textLabel 不会更改框架以适应新文本。我必须在页脚/页眉视图中调用sizeToFit 吗?
    • 您需要调用 beginUpdates/endUpdates 让表格视图重新计算其布局
    • 黄金! tableView.headerViewForSection(indexPath.section)?.textLabel.text = tableView(tableView, titleForHeaderInSection: indexPath.section)
    • 请注意,这不尊重操作系统的样式。例如应用于分组节标题的全大写字体。
    【解决方案2】:

    这应该可行:

        UIView.setAnimationsEnabled(false)
        tableView.beginUpdates()
    
        if let containerView = tableView.footerViewForSection(0) {
            containerView.textLabel!.text = "New footer text!"
    
            containerView.sizeToFit()
        }
    
        tableView.endUpdates()
        UIView.setAnimationsEnabled(true)
    

    【讨论】:

    • 它在 iOS 9、Swift 2.2 中运行良好。而且它比编码框架矩形更干净,当然应该避免。
    【解决方案3】:

    我设法以一种间接的方式做到了这一点:我创建了一个 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);
    }
    

    有人知道不设置自定义页脚视图的方法吗?

    【讨论】:

    • 这就是这样做的方法。每当您拨打 [sectionFooterView setText: @"Some text"] 时,它都会在屏幕上更新。
    【解决方案4】:

    这就是你在 Swift 3.0 中的做法

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

    【讨论】:

      【解决方案5】:

      这是另一种方法:

      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];
      

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 这可行,但它不是最有效的,因为它不仅会更新节页眉/页脚,还会更新所有单元格。似乎阿里的答案是要走的路。
        • 我认为苹果希望如果您更改页脚或页眉,内容也会发生变化。但是,是的,如果您想自定义的不仅仅是半静态显示的文本,那么使用 viewForFooter/Header 会更容易。请注意,默认的部分页眉和页脚为您做了很多格式化,并且像这样为一些琐碎的事情覆盖它们也可能不是最好的选择。
        【解决方案7】:

        这是一个 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】:

          我发现如果文本从填充一行变为多行再变为一行,则页脚文本不会正确更新。重置标签的宽度和高度可以解决这个问题。

              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】:

            在我的表格视图中重新加载带有reloadSections 的部分对我很有用

            【讨论】:

              【解决方案10】:

              你也可以这样做

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

              【讨论】:

              • 使用您的解决方案,您无法更改 titleForFooterInSection 之外的字符串。
              猜你喜欢
              • 2017-09-20
              • 1970-01-01
              • 2011-02-12
              • 2013-06-21
              • 1970-01-01
              • 1970-01-01
              • 2020-10-04
              • 2011-08-21
              相关资源
              最近更新 更多