我想在tableView header
中添加视图。我已经尝试了不同的方法,但是实现了目标。下面提供了我的代码,用于在headerView
中添加视图。
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return arrSupport[section]
}
func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
if let tableViewHeaderFooterView = view as? UITableViewHeaderFooterView {
tableViewHeaderFooterView.textLabel?.textColor = UIColor.white
tableViewHeaderFooterView.textLabel?.font = UIFont.systemFont(ofSize: 16, weight: .medium)
tableViewHeaderFooterView.contentView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
let headerView = UIView()
headerView.frame = CGRect(x: tblSupport.frame.origin.x-10, y: 10, width: tblSupport.frame.size.width-20, height: 60)
tableViewHeaderFooterView.contentView.frame = CGRect(x: 10, y: 0, width: tableView.frame.size.width-20, height: 50)
headerView.addSubview(tableViewHeaderFooterView)
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.frame = CGRect(x: tblSupport.frame.origin.x-10, y: 10, width: tblSupport.frame.size.width-20, height: 60)
headerView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
return headerView
}`
我也想通过单击相应的headerview来显示或隐藏tableview单元格,还需要像希望状态一样减小Headerview的宽度
当前状态:
所需状态:
答案 0 :(得分:1)
很简单,您可以通过以下方式实现它:
单击折叠按钮重新加载部分并将行数设为0
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60;
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
let label = UILabel()
let abutton = UIButton()
abutton.frame = CGRect(x: tableView.frame.size.width-60, y: 0, width: 40, height: 60)
abutton.setTitle("^", for: .normal)
abutton.setTitleColor(.gray, for: .normal)
abutton.addTarget(self, action: Selector(("collappesAndExpend")), for: .touchUpInside)
label.frame = CGRect(x: 10, y: 0, width: tableView.frame.size.width, height: 60)
label.text = "Your Title"
label.textColor = .gray
headerView.frame = CGRect(x: 0, y: 10, width: tableView.frame.size.width, height: 60)
headerView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
headerView.addSubview(label)
headerView.addSubview(abutton)
return headerView
}
func collappesAndExpend(_ sender: Any) {
//Relod you section
}
答案 1 :(得分:1)
首先要解决第一个问题,您需要指定标题的高度。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: tblSupport.frame.width - 20, height: 40)
headerView.backgroundColor = UIColor(red: 39.0 / 255.0, green: 41.0 / 255.0, blue: 53.0 / 255.0, alpha: 1.0)
return headerView
}
现在为您的第二个问题,我们将使像元高度最小,并通过调用beginUpdates()
和endUpdates()
,tableview
将制作动画,使您的像元缩小为动画方式:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView.init(frame: CGRect.init(x: 10, y: 10, width: tblSupport.frame.width - 20, height: 40)
let myButton = UIButton.init(frame: headerView.frame)
myButton.backgroundColor = .clear
myButton.tag = section
myButton.addTarget(self, action: #selector(buttonPressed(sender:)), for: .touchUpInside)
headerView.addSubview(myButton)
return headerView
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// add the next if statement
if indexPath.section == chosenSection {
return 0.0000001
}
return 30 // put your usual cell height here
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
var chosenSection = -1
@objc func buttonPressed(sender : UIButton) {
if chosenSection == sender.tag {
chosenSection = -1
} else {
chosenSection = sender.tag
tblSupport.beginUpdates()
tblSupport.endUpdates()
}
}
注意:将行高设置为0不会将其设置为0,而是将其设置为默认高度,这就是为什么我写了0.000001