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

前端 未结 10 609
轮回少年
轮回少年 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:02

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

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

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

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

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

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

    0 讨论(0)
提交回复
热议问题