工作之余写了一个类似拉钩首页demo,由于是在项目里面写的,所以没有单独摘出demo,主要代码如下:
class LagouHomeController: UIViewController {
var topView = BannerView()
var toolView = ToolView()
var bgView = UIView()
var datas = [ApplyListModel]()
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0...10 {
let model = ApplyListModel(meetingName: "测试", meetingRoom:"1", useTime: "2019", joiner: "11", applyTime: "20", status: 1, isExpand: false)
datas.append(model)
}
self.view.backgroundColor = UIColor.white
bgView = UIView.init()
topView = BannerView.init(frame: CGRect.zero)
topView.backgroundColor = UIColor.red
toolView = ToolView.init(frame: CGRect.zero)
toolView.delegate = self
bgView.addSubview(topView)
bgView.addSubview(toolView)
topView.snp.makeConstraints { (make) in
make.leading.top.trailing.equalTo(0)
}
toolView.snp.makeConstraints { (make) in
make.leading.trailing.equalTo(0)
make.top.equalTo(topView.snp_bottomMargin).offset(10)
make.height.equalTo(60)
make.bottom.equalTo(-10)
}
tableView = UITableView.init()
tableView.delegate = self
tableView.dataSource = self
tableView .register(MyApplyListCell.self, forCellReuseIdentifier: "MyApplyListCell")
tableView.tableHeaderView = bgView
tableView.tableHeaderView?.qmui_height = bgView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
tableView.estimatedRowHeight = 180
tableView.rowHeight = UITableView.automaticDimension
self.view.addSubview(tableView)
tableView.snp.makeConstraints{ (make) in
make.leading.trailing.top.bottom.equalTo(0)
}
}
}
extension LagouHomeController:UITableViewDelegate,UITableViewDataSource,ToolViewDelegate{
func scrollToTop() {
toolView.removeFromSuperview()
bgView.addSubview(toolView)
topView.snp.remakeConstraints { (make) in
make.leading.top.trailing.equalTo(0)
}
toolView.snp.remakeConstraints { (make) in
make.leading.trailing.equalTo(0)
make.top.equalTo(topView.snp_bottomMargin).offset(10)
make.height.equalTo(60)
make.bottom.equalTo(-10)
}
tableView.tableHeaderView = bgView
tableView.tableHeaderView?.qmui_height = bgView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height;
tableView.snp.remakeConstraints { (make) in
make.leading.trailing.top.bottom.equalTo(0)
}
tableView.qmui_scrollToTop()
tableView.reloadData()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//根据偏移量来修改tableView的headerView
let offsetY = scrollView.contentOffset.y
print(offsetY)
if offsetY > 0 {
let alpha = (400 - offsetY)/400
if alpha == 0 {
self.navigationController?.navigationBar.isHidden = true
}else{
self.navigationController?.navigationBar.isHidden = false
}
}
if offsetY > 300 {
toolView.changeframe()
bgView.removeFromSuperview()
self.view.addSubview(toolView)
toolView.snp.makeConstraints { (make) in
make.leading.trailing.top.equalTo(0)
make.height.equalTo(100)
}
tableView.tableHeaderView = nil
self.tableView.snp.remakeConstraints { (make) in
make.top.equalTo(toolView.snp_bottomMargin).offset(0)
make.leading.trailing.bottom.equalTo(0)
}
self.view.setNeedsLayout()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.datas.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyApplyListCell", for: indexPath) as! MyApplyListCell
cell.model = datas[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView .deselectRow(at: indexPath, animated: true)
var model = datas[indexPath.row]
model.isExpand = !model.isExpand
datas[indexPath.row] = model
tableView.reloadRows(at: [indexPath], with: .none)
}
}