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

只谈情不闲聊 提交于 2019-11-26 16:26:42

问题


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 cells in a table view's section in its section footer. The table view is editable and I delete or insert rows using

– insertRowsAtIndexPaths:withRowAnimation:
– deleteRowsAtIndexPaths:withRowAnimation:

It seems that these methods do not update the section footer. Strangely, when I call these methods the table view data-source method

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

is called (twice!) but it does not update the table view with the new values!!

Anyone has any idea how to fix this problem?


回答1:


If you just have a basic text header or footer, you can access them directly:

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

similarly for the header:

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



回答2:


This should work:

    UIView.setAnimationsEnabled(false)
    tableView.beginUpdates()

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

        containerView.sizeToFit()
    }

    tableView.endUpdates()
    UIView.setAnimationsEnabled(true)



回答3:


I managed to do it in an indirect way: I created a UILabel and set it as section header/footer.

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

Does anyone know a way to do this without setting a custom footer view?




回答4:


This is how you do it in Swift 3.0

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



回答5:


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



回答6:


Check the documentation for UITableView, or more specifically -reloadSections:withRowAnimation:




回答7:


You can also do it this way

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



回答8:


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



回答9:


I have found that the footer text doesn't update correctly if the text changes from filling one line to multiple ones back to one line. Reseting the label width and height would solve this issue.

    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)


来源:https://stackoverflow.com/questions/4346944/changing-uitableviews-section-header-footer-title-without-reloading-the-whole-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!