Changing UITableView's section header/footer t

2019-01-11 09:48发布

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?

7条回答
smile是对你的礼貌
2楼-- · 2019-01-11 10:10

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];
查看更多
Lonely孤独者°
3楼-- · 2019-01-11 10:11

This is how you do it in Swift 3.0

tableView.beginUpdates()
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text"
tableView.endUpdates()
查看更多
冷血范
4楼-- · 2019-01-11 10:11

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

查看更多
小情绪 Triste *
5楼-- · 2019-01-11 10:11

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 ""
    }
}
查看更多
Melony?
6楼-- · 2019-01-11 10:12

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?

查看更多
Rolldiameter
7楼-- · 2019-01-11 10:13

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)
查看更多
登录 后发表回答