点击tableViewHeader控制cell关闭展开以及label的代码自定义约束
来源:互联网 发布:淘宝访客计算方式 编辑:程序博客网 时间:2024/06/16 01:42
// Arrow label
contentView.addSubview(arrowLabel)
arrowLabel.textColor =UIColor.white
arrowLabel.translatesAutoresizingMaskIntoConstraints =false
arrowLabel.widthAnchor.constraint(equalToConstant:12).isActive =true
arrowLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
arrowLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
arrowLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
//
// Call tapHeader when tapping on this header
//
addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))
完整代码:
import UIKit
protocol CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header:CollapsibleTableViewHeader, section:Int)
}
class CollapsibleTableViewHeader:UITableViewHeaderFooterView {
var delegate:CollapsibleTableViewHeaderDelegate?
var section:Int = 0
let titleLabel =UILabel()
let arrowLabel =UILabel()
overrideinit(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
// Content View
contentView.backgroundColor =UIColor(hex:0x2E3944)
let marginGuide =contentView.layoutMarginsGuide
// Arrow label
contentView.addSubview(arrowLabel)
arrowLabel.textColor =UIColor.white
arrowLabel.translatesAutoresizingMaskIntoConstraints =false
arrowLabel.widthAnchor.constraint(equalToConstant:12).isActive =true
arrowLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
arrowLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
arrowLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
// Title label
contentView.addSubview(titleLabel)
titleLabel.textColor =UIColor.white
titleLabel.translatesAutoresizingMaskIntoConstraints =false
titleLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
titleLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true
titleLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
titleLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
//
// Call tapHeader when tapping on this header
//
addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))
}
requiredinit?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
//
// Trigger toggle section when tapping on the header
//
func tapHeader(_ gestureRecognizer:UITapGestureRecognizer) {
guardlet cell = gestureRecognizer.viewas? CollapsibleTableViewHeaderelse {
return
}
delegate?.toggleSection(self, section: cell.section)
}
func setCollapsed(_ collapsed:Bool) {
//
// Animate the arrow rotation (see Extensions.swf)
//
arrowLabel.rotate(collapsed ?0.0 : .pi /2)
}
}
extension UIView {
func rotate(_ toValue:CGFloat, duration: CFTimeInterval = 0.2) {
let animation =CABasicAnimation(keyPath:"transform.rotation")
animation.toValue = toValue
animation.duration = duration
animation.isRemovedOnCompletion =false
animation.fillMode =kCAFillModeForwards
self.layer.add(animation, forKey:nil)
}
}
import UIKit
//
// MARK: - View Controller
//
class CollapsibleTableViewController:UITableViewController {
var sections =sectionsData
overridefunc viewDidLoad() {
super.viewDidLoad()
// Auto resizing the height of the cell
tableView.estimatedRowHeight =44.0
tableView.rowHeight =UITableViewAutomaticDimension
self.title ="Apple Products"
}
}
//
// MARK: - View Controller DataSource and Delegate
//
extension CollapsibleTableViewController {
overridefunc numberOfSections(in tableView:UITableView) -> Int {
returnsections.count
}
overridefunc tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
returnsections[section].collapsed ?0 : sections[section].items.count
}
// Cell
overridefunc tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {
let cell:CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier:"cell") as?CollapsibleTableViewCell ??
CollapsibleTableViewCell(style: .default, reuseIdentifier:"cell")
let item:Item = sections[indexPath.section].items[indexPath.row]
cell.nameLabel.text = item.name
cell.detailLabel.text = item.detail
return cell
}
overridefunc tableView(_ tableView:UITableView, heightForRowAt indexPath:IndexPath) -> CGFloat {
returnUITableViewAutomaticDimension
}
// Header
overridefunc tableView(_ tableView:UITableView, viewForHeaderInSection section:Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier:"header") as?CollapsibleTableViewHeader ??CollapsibleTableViewHeader(reuseIdentifier:"header")
header.titleLabel.text =sections[section].name
header.arrowLabel.text =">"
header.setCollapsed(sections[section].collapsed)
header.section = section
header.delegate =self
return header
}
overridefunc tableView(_ tableView:UITableView, heightForHeaderInSection section:Int) -> CGFloat {
return44.0
}
overridefunc tableView(_ tableView:UITableView, heightForFooterInSection section:Int) -> CGFloat {
return1.0
}
}
//
// MARK: - Section Header Delegate
//
extension CollapsibleTableViewController:CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header:CollapsibleTableViewHeader, section:Int) {
let collapsed =!sections[section].collapsed
// Toggle collapse
sections[section].collapsed = collapsed
header.setCollapsed(collapsed)
tableView.reloadSections(NSIndexSet(index: section)as IndexSet, with: .automatic)
}
}
- 点击tableViewHeader控制cell关闭展开以及label的代码自定义约束
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- tableview 自定义cell 点击cell改变cell中的label.text的字体颜色,cell复用出现问题
- 关于tableviewHeader盖住cell的问题解决
- div+js的伸缩控制代码(展开和关闭)
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 点击UITableView的cell展开收缩
- 关于cell点击展开收缩的总结
- UITableView点击展开cell
- 最简单的点击展开关闭效果
- 自定义约束下的cell的高度
- 使用约束达到cell的自定义高度
- 点击展开关闭效果
- 搭建ionic项目环境并开始第一个demo
- oracle 把A用户所有表的查看权限赋给B用户(批量赋权)
- effective C++条款十九解读
- Eexecutor异步执行框架
- Paired Up
- 点击tableViewHeader控制cell关闭展开以及label的代码自定义约束
- ARM指令集
- python 2.7 在windows 下使用pip install xxx出现unicodeDecodeError解决办法
- WPF软件正版激活功能的实现
- python之argparse
- 数组去重的2种方法
- GIT使用前的安装
- Android热门前沿相关面试知识
- python