我有一个可扩展的UITableView
,具有特定的单元格、页眉和页脚高度。当用户点击标题时,单元格开始显示在其下方(部分展开)。当用户再次点击时,部分会折叠。
我的问题是,当用户点击标题时,标题变为绿色,箭头 (UIImageView
) 方向发生变化。当我不使用 dequeueReusableHeaderFooterView
时,一切都很完美,但是当我重复使用标题时,绿色标题和箭头方向在点击或滚动时看起来不像预期的那样。
在下图中,纽约的标题颜色看起来不错,但箭头方向错误。另外,曼哈顿,标题已展开,但没有变为绿色和正确的 UIImageView
方向。
P.S:我知道这个问题已经被问过很多次了,但我不知道哪一个是正确的方法。
标题 View 类:
protocol ExpandableHeaderViewDelegate {
func toggleSection(header: DistrictTableViewHeader, section: Int)
}
class DistrictTableViewHeader: UITableViewHeaderFooterView {
var delegate: ExpandableHeaderViewDelegate?
var section: Int!
let nameLabel: UILabel = {
let l = UILabel()
l.textColor = Color.DistrictsPage.headerTextColor
return l
}()
private let arrowImage: UIImageView = {
let i = UIImageView()
let image = UIImage(named: "ileri")?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
i.image = image
i.contentMode = .scaleAspectFit
return i
}()
var isColapsed: Bool!{
didSet{
layoutSubviews()
}
}
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderAction)))
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.font = UIFont.systemFont(ofSize: 22)
nameLabel.textColor = Color.DistrictsPage.headerTextColor
contentView.addSubview(nameLabel)
nameLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
nameLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 15).isActive = true
arrowImage.tintColor = UIColor(red:0.32, green:0.36, blue:0.36, alpha:1.0)
arrowImage.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(arrowImage)
arrowImage.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
arrowImage.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -20).isActive = true
arrowImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer) {
let cell = gestureRecognizer.view as! DistrictTableViewHeader
self.isColapsed = !isColapsed
if(!isColapsed){
let degrees : Double = 90 //the value in degrees
UIView.animate(withDuration: 0.5) { [weak self] in
self?.nameLabel.textColor = Color.Common.garantiLightGreen
self?.arrowImage.tintColor = Color.Common.garantiLightGreen
self?.arrowImage.rotate(CGFloat(degrees * .pi/180))
self?.contentView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
}
}else{
let degrees : Double = 0 //the value in degrees
UIView.animate(withDuration: 0.5) { [weak self] in
self?.nameLabel.textColor = Color.DistrictsPage.headerTextColor
self?.arrowImage.tintColor = UIColor.black
self?.arrowImage.rotate(CGFloat(degrees * .pi/180))
self?.contentView.backgroundColor = UIColor.white
}
}
delegate?.toggleSection(header: self, section: cell.section)
}
func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate) {
self.nameLabel.text = title
self.nameLabel.accessibilityIdentifier = title
self.section = section
self.delegate = delegate
}
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.backgroundColor = UIColor.white
}
}
如何初始化 header :
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header: DistrictTableViewHeader = tableView.dequeueReusableHeaderFooterView(withIdentifier: headerId) as! DistrictTableViewHeader
//let header = DistrictTableViewHeader()
header.isColapsed = !self.cities[section].isExpanded
header.customInit(title: self.cities[section].name, section: section, delegate: self)
return header
}
我如何展开/折叠:
func toggleSection(header: DistrictTableViewHeader, section: Int) {
self.cities[section].isExpanded = !self.cities[section].isExpanded
let contentOffset = self.tableView.contentOffset
self.tableView.reloadData()
}
编辑: 对于 UITableView 专家,我还添加了一个示例项目:) 示例项目链接:
最佳答案
将可重用 View 出列后,您必须重置可能对其发生的所有更改。
在这种情况下,您正在设置 header.isColapsed
但这不会重置所有需要的 View 状态。重用 View 时,还需要调用 selectHeaderAction
中的 View 状态更改代码。
需要进行一些重构才能做到正确。更改 isColapse
setter 以执行更多操作:
var isColapsed: Bool!{
didSet{
if(!isColapsed){
let degrees : Double = 90 //the value in degrees
self?.nameLabel.textColor = Color.Common.garantiLightGreen
self?.arrowImage.tintColor = Color.Common.garantiLightGreen
self?.arrowImage.rotate(CGFloat(degrees * .pi/180))
self?.contentView.backgroundColor = UIColor(red:0.97, green:0.97, blue:0.97, alpha:1.0)
}else{
let degrees : Double = 0 //the value in degrees
self?.nameLabel.textColor = Color.DistrictsPage.headerTextColor
self?.arrowImage.tintColor = UIColor.black
self?.arrowImage.rotate(CGFloat(degrees * .pi/180))
self?.contentView.backgroundColor = UIColor.white
}
layoutSubviews()
}
}
并减少点击手势的作用:
@objc func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer) {
let cell = gestureRecognizer.view as! DistrictTableViewHeader
UIView.animate(withDuration: 0.5) { [weak self] in
self.isColapsed = !isColapsed
}
delegate?.toggleSection(header: self, section: cell.section)
}
我认为这应该解决它。告诉我它是否有效。
<小时/>您的旋转方式错误。添加 CAAnimation 很酷,但是当 View 被重用时你不能轻易取消它。相反,只需更改 transform
属性并让 UIView.animate block 在更高级别处理动画。
func rotate(_ toValue: CGFloat) {
self.transform = CGAffineTransform.init(rotationAngle: toValue)
}
<小时/>
接下来,您将重新加载表格,这将取消您想要执行的所有动画。
func toggleSection(header: DistrictTableViewHeader, section: Int) {
self.cities[section].isExpanded = !self.cities[section].isExpanded
let count = self.cities[section].districts.count
let indexPaths:[IndexPath] = (0..<count).map{ IndexPath.init(row: $0, section: section) }
if self.cities[section].isExpanded {
self.tableView.insertRows(at: indexPaths, with: .automatic);
}else{
self.tableView.deleteRows(at: indexPaths, with: .automatic);
}
}
关于ios - 可重用 UITableHeaderFooterView 松散 ImageView 方向状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460005/