我对自己的代码有疑问。
我想在单击按钮时更改UITableView标头中的视图。
但是我不知道如何重置标题视图。
而且我也不怎么删除以前的标题视图并设置新的标题视图。
我想在单击按钮时切换标题视图。
对我有什么主意吗?
谢谢
我尝试编写示例代码。
以及如何在函数“ btnClicked”中更改标题视图。
enum ChartType {
case triangle
case circle
}
class ViewController: UIViewController {
let tableView: UITableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
let sectionHeader: UIView = creatChartView()
sectionHeader.backgroundColor = .white
tableView.tableHeaderView = sectionHeader
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 50
view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
make.top.left.right.bottom.equalToSuperview()
}
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let headerView = tableView.tableHeaderView {
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var headerFrame = headerView.frame
//Comparison necessary to avoid infinite loop
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
tableView.tableHeaderView = headerView
}
}
}
func creatChartView(type: ChartType = .circle) -> UIView {
let view = UIView()
switch type {
case .circle:
let titleLabel: UILabel = { () -> UILabel in
let ui = UILabel()
ui.text = "circle"
ui.textAlignment = .center
return ui
}()
let button: UIButton = { () -> UIButton in
let ui = UIButton()
ui.setTitle("Switch type", for: .normal)
ui.addTarget(self, action: #selector(btnClicked), for: .touchUpInside)
ui.setTitleColor(.red, for: .normal)
return ui
}()
let circleView = UIView()
let circlePath = UIBezierPath(arcCenter: CGPoint(x: UIScreen.main.bounds.size.width/2,y: 0), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 3.0
circleView.layer.addSublayer(shapeLayer)
view.addSubview(titleLabel)
view.addSubview(button)
view.addSubview(circleView)
titleLabel.snp.makeConstraints { (make) in
make.top.left.equalTo(15)
}
button.snp.makeConstraints { (make) in
make.right.equalTo(-15)
make.width.equalTo(titleLabel.snp.width)
make.height.equalTo(titleLabel.snp.height)
}
circleView.snp.makeConstraints { (make) in
make.top.equalTo(titleLabel.snp.bottom)
make.left.right.bottom.equalToSuperview()
make.height.equalTo(50)
}
return view
case .triangle:
let titleLabel: UILabel = { () -> UILabel in
let ui = UILabel()
ui.text = "triangle"
ui.textAlignment = .center
return ui
}()
let triangleView = UIView()
let triangle = TriangleView(frame: CGRect(x: 10, y: 20, width: 25 , height: 30))
triangle.backgroundColor = .red
triangleView.addSubview(triangle)
view.addSubview(titleLabel)
view.addSubview(triangleView)
titleLabel.snp.makeConstraints { (make) in
make.top.left.equalTo(15)
make.right.equalTo(-15)
make.height.equalTo(20)
}
triangleView.snp.makeConstraints { (make) in
make.top.equalTo(titleLabel.snp.bottom).offset(5)
make.left.right.bottom.equalToSuperview()
}
return view
}
}
@objc func btnClicked(sender: UIButton) {
//How to refresh the headerView
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Hello \(indexPath.row)"
return cell
}
}
class TriangleView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
context.beginPath()
context.move(to: CGPoint(x: rect.minX, y: rect.maxY))
context.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY))
context.addLine(to: CGPoint(x: (rect.maxX / 2.0), y: rect.minY))
context.closePath()
context.setFillColor(red: 1.0, green: 0.5, blue: 0.0, alpha: 0.60)
context.fillPath()
}
}
答案 0 :(得分:0)
首先,您需要一个标志来检查您当前在headerView
上的位置。
在此标志内,您可以使用属性观察器didSet
重新加载tableView
,只要此标志发生变化
var isNewHeaderView = false {
didSet {
tableView.reloadData()
}
}
现在在委托方法viewForHeaderInSection
中,您可以检查该标志并返回headerView
所需的类似内容
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if isNewHeaderView {
return UIView() // first View for header in section
} else {
return UIView() // second View for header in section
}
}
在btn
动作中,您可以像这样切换标志的值
@objc func btnClicked(sender: UIButton) {
isNewHeaderView = !isNewHeaderView
}
更新
在Swift 4.2中,您可以使用
isNewHeaderView.toggle()
现在,每次UIViewController
上的标志更改时,它将自动重新加载tableView
。