UITableView - Reposition Cells after Header Height change





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Problem



tableView does not reposition cells correctly after tableHeaderView height change.



Desired Outcome



after pressing a button, the tableHeaderView changes height, and correctly repositions cells.



Important Code




  • Changes height on tap - end of configureRxSwift()

  • Initialization of tableView - initTableView() & initTableHeader()


Current Outcome



enter image description hereenter image description here



Full code



import UIKit
import SwiftyJSON
import RxSwift
import RxCocoa
import SnapKit

class AthleteSearchController: UIViewController {

let tableView = UITableView()
var searchBar = UISearchBar()
var shouldShowSearchResults = false
var schoolID = ""
var sportMode = ""
let selectedAthlete = PublishSubject<JSON>()
let disposeBag = DisposeBag()
let filterButton = UIButton()
let tableHeaderView = UIView()
let filterView = UIView()
let searchView = UIView()

var filterShown = false
override func viewDidLoad() {
super.viewDidLoad()
initUI()
configureRxSwift()
}
func initUI() {
initTableView()
initTableHeader()
initSearchView()
initSearchBar()
initFilterButton()
initFilterView()
}
func initTableHeader() {
tableHeaderView.addSubview(filterView)
tableHeaderView.addSubview(searchView)

tableHeaderView.snp.makeConstraints { make in
make.width.equalTo(self.tableView.snp.width)
make.height.equalTo(50)
}

}
func initSearchView() {
searchView.addSubview(searchBar)
searchView.addSubview(filterButton)
searchView.snp.makeConstraints { make in
make.height.equalTo(50)
make.top.equalToSuperview()
make.width.equalToSuperview()
}
}
func initFilterButton() {
filterButton.setTitle("Filter", for: .normal)
filterButton.snp.makeConstraints { make in
make.top.bottom.right.equalToSuperview()
make.width.equalTo(70)
}
}
func initFilterView() {
filterView.backgroundColor = .red
filterView.snp.makeConstraints { make in
make.top.equalTo(searchView.snp.bottom)
make.bottom.width.equalToSuperview()
}
}
func initSearchBar() {
searchBar.placeholder = "Search for an athlete here..."

searchBar.delegate = nil
searchBar.sizeToFit()
searchBar.snp.makeConstraints { make in
make.left.top.bottom.equalToSuperview()
make.right.equalTo(self.filterButton.snp.left)
}
}

func initTableView() {
self.view.addSubview(tableView)
tableView.dataSource = nil
tableView.delegate = nil
tableView.snp.makeConstraints { make in
make.edges.equalTo(self.view.safeAreaLayoutGuide)

}
tableView.tableHeaderView = tableHeaderView

tableView.estimatedRowHeight = 40
tableView.rowHeight = UITableView.automaticDimension
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
func configureRxSwift() {

let url = "https://www.athletic.net/(self.sportMode)/School.aspx?SchoolID=(self.schoolID)"
let allAthletes = dataRequest(url: url).map {
$0[1]["athletes"].arrayValue
}
let searchFilter = self.searchBar.rx.text.orEmpty.asObservable()

Observable.combineLatest(allAthletes, searchFilter) { athletes, text in
text.isEmpty ? athletes : athletes.filter {
$0["Name"].stringValue.range(of: text, options: .caseInsensitive) != nil
}
}.bind(to: self.tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { (row, element, cell) in
cell.textLabel?.text = element["Name"].stringValue
}.disposed(by: disposeBag)

self.tableView.rx.modelSelected(JSON.self)
.debug("selectedAthlete")
.take(1)
.bind(to: self.selectedAthlete)
.disposed(by: disposeBag)

// Updates Height
filterButton.rx.tap.subscribe(onNext: {
self.tableHeaderView.snp.updateConstraints { make in
if (self.filterShown) {
make.height.equalTo(50)
} else {
make.height.equalTo(100)
}
self.filterShown.toggle()
self.tableView.tableHeaderView = self.tableHeaderView
}
})

}
override func viewWillDisappear(_ animated: Bool) {
self.selectedAthlete.onCompleted()
}
}









share|improve this question





























    0















    Problem



    tableView does not reposition cells correctly after tableHeaderView height change.



    Desired Outcome



    after pressing a button, the tableHeaderView changes height, and correctly repositions cells.



    Important Code




    • Changes height on tap - end of configureRxSwift()

    • Initialization of tableView - initTableView() & initTableHeader()


    Current Outcome



    enter image description hereenter image description here



    Full code



    import UIKit
    import SwiftyJSON
    import RxSwift
    import RxCocoa
    import SnapKit

    class AthleteSearchController: UIViewController {

    let tableView = UITableView()
    var searchBar = UISearchBar()
    var shouldShowSearchResults = false
    var schoolID = ""
    var sportMode = ""
    let selectedAthlete = PublishSubject<JSON>()
    let disposeBag = DisposeBag()
    let filterButton = UIButton()
    let tableHeaderView = UIView()
    let filterView = UIView()
    let searchView = UIView()

    var filterShown = false
    override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
    configureRxSwift()
    }
    func initUI() {
    initTableView()
    initTableHeader()
    initSearchView()
    initSearchBar()
    initFilterButton()
    initFilterView()
    }
    func initTableHeader() {
    tableHeaderView.addSubview(filterView)
    tableHeaderView.addSubview(searchView)

    tableHeaderView.snp.makeConstraints { make in
    make.width.equalTo(self.tableView.snp.width)
    make.height.equalTo(50)
    }

    }
    func initSearchView() {
    searchView.addSubview(searchBar)
    searchView.addSubview(filterButton)
    searchView.snp.makeConstraints { make in
    make.height.equalTo(50)
    make.top.equalToSuperview()
    make.width.equalToSuperview()
    }
    }
    func initFilterButton() {
    filterButton.setTitle("Filter", for: .normal)
    filterButton.snp.makeConstraints { make in
    make.top.bottom.right.equalToSuperview()
    make.width.equalTo(70)
    }
    }
    func initFilterView() {
    filterView.backgroundColor = .red
    filterView.snp.makeConstraints { make in
    make.top.equalTo(searchView.snp.bottom)
    make.bottom.width.equalToSuperview()
    }
    }
    func initSearchBar() {
    searchBar.placeholder = "Search for an athlete here..."

    searchBar.delegate = nil
    searchBar.sizeToFit()
    searchBar.snp.makeConstraints { make in
    make.left.top.bottom.equalToSuperview()
    make.right.equalTo(self.filterButton.snp.left)
    }
    }

    func initTableView() {
    self.view.addSubview(tableView)
    tableView.dataSource = nil
    tableView.delegate = nil
    tableView.snp.makeConstraints { make in
    make.edges.equalTo(self.view.safeAreaLayoutGuide)

    }
    tableView.tableHeaderView = tableHeaderView

    tableView.estimatedRowHeight = 40
    tableView.rowHeight = UITableView.automaticDimension
    self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    func configureRxSwift() {

    let url = "https://www.athletic.net/(self.sportMode)/School.aspx?SchoolID=(self.schoolID)"
    let allAthletes = dataRequest(url: url).map {
    $0[1]["athletes"].arrayValue
    }
    let searchFilter = self.searchBar.rx.text.orEmpty.asObservable()

    Observable.combineLatest(allAthletes, searchFilter) { athletes, text in
    text.isEmpty ? athletes : athletes.filter {
    $0["Name"].stringValue.range(of: text, options: .caseInsensitive) != nil
    }
    }.bind(to: self.tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { (row, element, cell) in
    cell.textLabel?.text = element["Name"].stringValue
    }.disposed(by: disposeBag)

    self.tableView.rx.modelSelected(JSON.self)
    .debug("selectedAthlete")
    .take(1)
    .bind(to: self.selectedAthlete)
    .disposed(by: disposeBag)

    // Updates Height
    filterButton.rx.tap.subscribe(onNext: {
    self.tableHeaderView.snp.updateConstraints { make in
    if (self.filterShown) {
    make.height.equalTo(50)
    } else {
    make.height.equalTo(100)
    }
    self.filterShown.toggle()
    self.tableView.tableHeaderView = self.tableHeaderView
    }
    })

    }
    override func viewWillDisappear(_ animated: Bool) {
    self.selectedAthlete.onCompleted()
    }
    }









    share|improve this question

























      0












      0








      0








      Problem



      tableView does not reposition cells correctly after tableHeaderView height change.



      Desired Outcome



      after pressing a button, the tableHeaderView changes height, and correctly repositions cells.



      Important Code




      • Changes height on tap - end of configureRxSwift()

      • Initialization of tableView - initTableView() & initTableHeader()


      Current Outcome



      enter image description hereenter image description here



      Full code



      import UIKit
      import SwiftyJSON
      import RxSwift
      import RxCocoa
      import SnapKit

      class AthleteSearchController: UIViewController {

      let tableView = UITableView()
      var searchBar = UISearchBar()
      var shouldShowSearchResults = false
      var schoolID = ""
      var sportMode = ""
      let selectedAthlete = PublishSubject<JSON>()
      let disposeBag = DisposeBag()
      let filterButton = UIButton()
      let tableHeaderView = UIView()
      let filterView = UIView()
      let searchView = UIView()

      var filterShown = false
      override func viewDidLoad() {
      super.viewDidLoad()
      initUI()
      configureRxSwift()
      }
      func initUI() {
      initTableView()
      initTableHeader()
      initSearchView()
      initSearchBar()
      initFilterButton()
      initFilterView()
      }
      func initTableHeader() {
      tableHeaderView.addSubview(filterView)
      tableHeaderView.addSubview(searchView)

      tableHeaderView.snp.makeConstraints { make in
      make.width.equalTo(self.tableView.snp.width)
      make.height.equalTo(50)
      }

      }
      func initSearchView() {
      searchView.addSubview(searchBar)
      searchView.addSubview(filterButton)
      searchView.snp.makeConstraints { make in
      make.height.equalTo(50)
      make.top.equalToSuperview()
      make.width.equalToSuperview()
      }
      }
      func initFilterButton() {
      filterButton.setTitle("Filter", for: .normal)
      filterButton.snp.makeConstraints { make in
      make.top.bottom.right.equalToSuperview()
      make.width.equalTo(70)
      }
      }
      func initFilterView() {
      filterView.backgroundColor = .red
      filterView.snp.makeConstraints { make in
      make.top.equalTo(searchView.snp.bottom)
      make.bottom.width.equalToSuperview()
      }
      }
      func initSearchBar() {
      searchBar.placeholder = "Search for an athlete here..."

      searchBar.delegate = nil
      searchBar.sizeToFit()
      searchBar.snp.makeConstraints { make in
      make.left.top.bottom.equalToSuperview()
      make.right.equalTo(self.filterButton.snp.left)
      }
      }

      func initTableView() {
      self.view.addSubview(tableView)
      tableView.dataSource = nil
      tableView.delegate = nil
      tableView.snp.makeConstraints { make in
      make.edges.equalTo(self.view.safeAreaLayoutGuide)

      }
      tableView.tableHeaderView = tableHeaderView

      tableView.estimatedRowHeight = 40
      tableView.rowHeight = UITableView.automaticDimension
      self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
      }
      func configureRxSwift() {

      let url = "https://www.athletic.net/(self.sportMode)/School.aspx?SchoolID=(self.schoolID)"
      let allAthletes = dataRequest(url: url).map {
      $0[1]["athletes"].arrayValue
      }
      let searchFilter = self.searchBar.rx.text.orEmpty.asObservable()

      Observable.combineLatest(allAthletes, searchFilter) { athletes, text in
      text.isEmpty ? athletes : athletes.filter {
      $0["Name"].stringValue.range(of: text, options: .caseInsensitive) != nil
      }
      }.bind(to: self.tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { (row, element, cell) in
      cell.textLabel?.text = element["Name"].stringValue
      }.disposed(by: disposeBag)

      self.tableView.rx.modelSelected(JSON.self)
      .debug("selectedAthlete")
      .take(1)
      .bind(to: self.selectedAthlete)
      .disposed(by: disposeBag)

      // Updates Height
      filterButton.rx.tap.subscribe(onNext: {
      self.tableHeaderView.snp.updateConstraints { make in
      if (self.filterShown) {
      make.height.equalTo(50)
      } else {
      make.height.equalTo(100)
      }
      self.filterShown.toggle()
      self.tableView.tableHeaderView = self.tableHeaderView
      }
      })

      }
      override func viewWillDisappear(_ animated: Bool) {
      self.selectedAthlete.onCompleted()
      }
      }









      share|improve this question














      Problem



      tableView does not reposition cells correctly after tableHeaderView height change.



      Desired Outcome



      after pressing a button, the tableHeaderView changes height, and correctly repositions cells.



      Important Code




      • Changes height on tap - end of configureRxSwift()

      • Initialization of tableView - initTableView() & initTableHeader()


      Current Outcome



      enter image description hereenter image description here



      Full code



      import UIKit
      import SwiftyJSON
      import RxSwift
      import RxCocoa
      import SnapKit

      class AthleteSearchController: UIViewController {

      let tableView = UITableView()
      var searchBar = UISearchBar()
      var shouldShowSearchResults = false
      var schoolID = ""
      var sportMode = ""
      let selectedAthlete = PublishSubject<JSON>()
      let disposeBag = DisposeBag()
      let filterButton = UIButton()
      let tableHeaderView = UIView()
      let filterView = UIView()
      let searchView = UIView()

      var filterShown = false
      override func viewDidLoad() {
      super.viewDidLoad()
      initUI()
      configureRxSwift()
      }
      func initUI() {
      initTableView()
      initTableHeader()
      initSearchView()
      initSearchBar()
      initFilterButton()
      initFilterView()
      }
      func initTableHeader() {
      tableHeaderView.addSubview(filterView)
      tableHeaderView.addSubview(searchView)

      tableHeaderView.snp.makeConstraints { make in
      make.width.equalTo(self.tableView.snp.width)
      make.height.equalTo(50)
      }

      }
      func initSearchView() {
      searchView.addSubview(searchBar)
      searchView.addSubview(filterButton)
      searchView.snp.makeConstraints { make in
      make.height.equalTo(50)
      make.top.equalToSuperview()
      make.width.equalToSuperview()
      }
      }
      func initFilterButton() {
      filterButton.setTitle("Filter", for: .normal)
      filterButton.snp.makeConstraints { make in
      make.top.bottom.right.equalToSuperview()
      make.width.equalTo(70)
      }
      }
      func initFilterView() {
      filterView.backgroundColor = .red
      filterView.snp.makeConstraints { make in
      make.top.equalTo(searchView.snp.bottom)
      make.bottom.width.equalToSuperview()
      }
      }
      func initSearchBar() {
      searchBar.placeholder = "Search for an athlete here..."

      searchBar.delegate = nil
      searchBar.sizeToFit()
      searchBar.snp.makeConstraints { make in
      make.left.top.bottom.equalToSuperview()
      make.right.equalTo(self.filterButton.snp.left)
      }
      }

      func initTableView() {
      self.view.addSubview(tableView)
      tableView.dataSource = nil
      tableView.delegate = nil
      tableView.snp.makeConstraints { make in
      make.edges.equalTo(self.view.safeAreaLayoutGuide)

      }
      tableView.tableHeaderView = tableHeaderView

      tableView.estimatedRowHeight = 40
      tableView.rowHeight = UITableView.automaticDimension
      self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
      }
      func configureRxSwift() {

      let url = "https://www.athletic.net/(self.sportMode)/School.aspx?SchoolID=(self.schoolID)"
      let allAthletes = dataRequest(url: url).map {
      $0[1]["athletes"].arrayValue
      }
      let searchFilter = self.searchBar.rx.text.orEmpty.asObservable()

      Observable.combineLatest(allAthletes, searchFilter) { athletes, text in
      text.isEmpty ? athletes : athletes.filter {
      $0["Name"].stringValue.range(of: text, options: .caseInsensitive) != nil
      }
      }.bind(to: self.tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)) { (row, element, cell) in
      cell.textLabel?.text = element["Name"].stringValue
      }.disposed(by: disposeBag)

      self.tableView.rx.modelSelected(JSON.self)
      .debug("selectedAthlete")
      .take(1)
      .bind(to: self.selectedAthlete)
      .disposed(by: disposeBag)

      // Updates Height
      filterButton.rx.tap.subscribe(onNext: {
      self.tableHeaderView.snp.updateConstraints { make in
      if (self.filterShown) {
      make.height.equalTo(50)
      } else {
      make.height.equalTo(100)
      }
      self.filterShown.toggle()
      self.tableView.tableHeaderView = self.tableHeaderView
      }
      })

      }
      override func viewWillDisappear(_ animated: Bool) {
      self.selectedAthlete.onCompleted()
      }
      }






      ios swift uitableview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 16:06









      PLZHELPPLZHELP

      71110




      71110
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You should reload the table view after changing header height:



          self.tableHeaderView.snp.updateConstraints { make in
          if (self.filterShown) {
          make.height.equalTo(50)
          } else {
          make.height.equalTo(100)
          }
          self.filterShown.toggle()
          self.tableView.tableHeaderView = self.tableHeaderView
          self.tableView.reloadData() // <- add this
          }







          share|improve this answer
























          • this adds some very weird behavior, and does not completely resolve the issue.

            – PLZHELP
            Jan 4 at 22:04











          • ok then, keep your secrets

            – PLZHELP
            Jan 23 at 21:28












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042415%2fuitableview-reposition-cells-after-header-height-change%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You should reload the table view after changing header height:



          self.tableHeaderView.snp.updateConstraints { make in
          if (self.filterShown) {
          make.height.equalTo(50)
          } else {
          make.height.equalTo(100)
          }
          self.filterShown.toggle()
          self.tableView.tableHeaderView = self.tableHeaderView
          self.tableView.reloadData() // <- add this
          }







          share|improve this answer
























          • this adds some very weird behavior, and does not completely resolve the issue.

            – PLZHELP
            Jan 4 at 22:04











          • ok then, keep your secrets

            – PLZHELP
            Jan 23 at 21:28
















          0














          You should reload the table view after changing header height:



          self.tableHeaderView.snp.updateConstraints { make in
          if (self.filterShown) {
          make.height.equalTo(50)
          } else {
          make.height.equalTo(100)
          }
          self.filterShown.toggle()
          self.tableView.tableHeaderView = self.tableHeaderView
          self.tableView.reloadData() // <- add this
          }







          share|improve this answer
























          • this adds some very weird behavior, and does not completely resolve the issue.

            – PLZHELP
            Jan 4 at 22:04











          • ok then, keep your secrets

            – PLZHELP
            Jan 23 at 21:28














          0












          0








          0







          You should reload the table view after changing header height:



          self.tableHeaderView.snp.updateConstraints { make in
          if (self.filterShown) {
          make.height.equalTo(50)
          } else {
          make.height.equalTo(100)
          }
          self.filterShown.toggle()
          self.tableView.tableHeaderView = self.tableHeaderView
          self.tableView.reloadData() // <- add this
          }







          share|improve this answer













          You should reload the table view after changing header height:



          self.tableHeaderView.snp.updateConstraints { make in
          if (self.filterShown) {
          make.height.equalTo(50)
          } else {
          make.height.equalTo(100)
          }
          self.filterShown.toggle()
          self.tableView.tableHeaderView = self.tableHeaderView
          self.tableView.reloadData() // <- add this
          }








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 4 at 20:08









          dduyduongdduyduong

          12615




          12615













          • this adds some very weird behavior, and does not completely resolve the issue.

            – PLZHELP
            Jan 4 at 22:04











          • ok then, keep your secrets

            – PLZHELP
            Jan 23 at 21:28



















          • this adds some very weird behavior, and does not completely resolve the issue.

            – PLZHELP
            Jan 4 at 22:04











          • ok then, keep your secrets

            – PLZHELP
            Jan 23 at 21:28

















          this adds some very weird behavior, and does not completely resolve the issue.

          – PLZHELP
          Jan 4 at 22:04





          this adds some very weird behavior, and does not completely resolve the issue.

          – PLZHELP
          Jan 4 at 22:04













          ok then, keep your secrets

          – PLZHELP
          Jan 23 at 21:28





          ok then, keep your secrets

          – PLZHELP
          Jan 23 at 21:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54042415%2fuitableview-reposition-cells-after-header-height-change%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas