1

I have UITableview With a section header. When the section header is sticky to the top of the View, I would like to refresh the data source of the table view, but I do not want the section header to be refreshed. Because of the section header has several buttons, I want to keep it the same

Can I only refresh the UITableView data source​? Or I have to put the section header out of the UITableView?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
benoitcn
  • 149
  • 1
  • 12
  • Possible duplicate of [Reload section without reloading section header](https://stackoverflow.com/questions/20802648/reload-section-without-reloading-section-header) – Arash Etemad Aug 03 '19 at 06:30

3 Answers3

0

Table View Delegate contains scroll view methods, so you can do your logic as following

func scrollViewDidScroll(_ scrollView: UIScrollView) {
     if scrollView.contentOffset.y == 0 {
         tableView.reloadData()
     }
}
  • I don’t want the section header to be reloaded. table view.reloadData would update everything. I only need to update all the cell in the section. – benoitcn Aug 03 '19 at 11:53
  • oh ok sorry. than use `tableView.reloadSections(IndexSet, with: UITableView.RowAnimation) ` instead of `tableView.reloadData()` – Yervand Saribekyan Aug 03 '19 at 12:54
  • It works exactly as what I want, only if the section header is not sticky to the top. When it is sticky to the top, `reloadSections` would make the button in the section header bounce a bit. I guess the section header was still being refreshed anyhow. – benoitcn Aug 04 '19 at 02:03
0

I try to use table.reloadSections(IndexSet(integer: 0), with: UITableView.RowAnimation.none) to reload the first section.

This is the screen record link bouncey section header content

benoitcn
  • 149
  • 1
  • 12
0

I found the solution is to remove the content from the tableview section header while the section header is sticky to the top. Add it to the view instead.

Then we can refresh the section or the tableview. If we scroll down then, put the header back to the section header container.

to remove the section header, when a button inside the header

    @objc func refreshSection(){
    if sectionHeaderStickyTop == true {
        guard let header = sectionHeaderContainer.subviews.first else {
            return
        }

        sectionHeaderStickyTop = false

        header.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(header)

        let guide = view.safeAreaLayoutGuide

        header.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
        header.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
        header.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
        header.heightAnchor.constraint(equalToConstant: header.bounds.size.height).isActive = true
    }

    table.reloadSections(IndexSet(integer: 0), with: UITableView.RowAnimation.none)
}

put the header back to the section header, if the header has been removed

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
    guard let tableView = scrollView as? UITableView else {
        return
    }

    let header = sectionHeader
    dummyButton.setTitle("\(Int.random(in: 1...100))", for: UIControl.State.normal)
    dummyButton.translatesAutoresizingMaskIntoConstraints = false
    dummyButton.addTarget(self, action: #selector(refreshSection), for: UIControl.Event.touchUpInside)

    let headerViewHeight = table.tableHeaderView?.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height ?? 0


    if scrollView.contentOffset.y >= headerViewHeight {
        addToHeader(header: header, addonView: dummyButton, height: 44, width: 80)
    }else{
        if sectionHeaderStickyTop == false {
            sectionHeaderStickyTop = true
            sectionHeader.translatesAutoresizingMaskIntoConstraints = true
            header.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
            header.frame = CGRect(x: 0, y: 0, width: header.bounds.width, height: header.bounds.height)
            sectionHeaderContainer.addSubview(sectionHeader)
        }
        dummyButton.removeFromSuperview()
    }
    tableView.setNeedsLayout()
}
benoitcn
  • 149
  • 1
  • 12