Changing UITableView's section header/footer title without reloading the whole table view

前端 未结 10 610
轮回少年
轮回少年 2020-12-03 04:47

Is there any way to reload the section header/footer of a table view without calling [tableView reloadData];?

In fact, I want to show the number of cel

相关标签:
10条回答
  • 2020-12-03 05:24

    Here's another way to do it:

    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];
    
    0 讨论(0)
  • 2020-12-03 05:24

    It worked for me to just reload the section with reloadSections on my table view

    0 讨论(0)
  • 2020-12-03 05:27

    Here's a UITableView extension that's a handy shortcut for refreshing the tableView header/footer titles, based on some of the answers above (note the artificial uppercasing of the header title).

    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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:28

    This is how you do it in Swift 3.0

    tableView.beginUpdates()
    tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text"
    tableView.endUpdates()
    
    0 讨论(0)
提交回复
热议问题