ios - 如何绑定 UITableView Header 并且还想通过在 swift 中以编程方式单击 tableview 标题来显示或隐藏 tableview 单元格
问题描述
我想在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的宽度,比如期望状态
当前状态:
期望状态:
解决方案
首先要解决您的第一个问题,您需要指定标题的高度。
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()
and 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 的原因
推荐阅读
- php - 导出 mysql db 时 mysql 表中的列默认值更改
- php - Laravel 和时间戳 - 它总是一个日期列吗?
- mobile - Bootstrap 4 在 Chrome 中没有响应
- reactjs - 使用 React Js 的最佳方式是什么:Redux、Hooks 或 Context?
- python - DataBricks:在 arrays_zip 函数中使用变量
- java - 如果其中没有奇数值,则返回 0 的数组
- java - 使用 maven surefire 测试花费的时间超过指定的超时时间
- apache-spark - 为什么使用for循环时python spark慢
- exception - 如何在用例图中显示异常?
- c# - 我是否需要使用实体框架将属性 int 显式添加到组合对象的外键中?