tableHeaderView高度更改后,
tableView不能正确地重新定位单元格。
按下按钮后,tableHeaderView会更改高度,并正确地重新定位单元格。
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()
}
}
答案 0 :(得分:0)
更改标题高度后,您应该重新加载表格视图:
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
}