当单击节标题但所有标题都在扩展而不是所选标题时,我一直在尝试折叠和展开collectionView中的单元格。 这是我的代码
class ExampleHomeController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var expandedRow: Bool = false
var section: Int!
let postCellId = "postCellId"
let headerCellId = "headerCellId"
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.backgroundColor = UIColor.init(r: 220, g: 214, b: 214)
collectionView.register(PostCell.self, forCellWithReuseIdentifier: postCellId)
collectionView.register(HeaderCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: headerCellId)
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: postCellId, for: indexPath)
return cell
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerCellId, for: indexPath) as! HeaderCell
header.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
return header
}
@objc func showCells() {
if expandedRow == false {
expandedRow = true
}else {
expandedRow = false
}
collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if expandedRow == true {
return CGSize(width: view.frame.width - 24, height: 280)
} else {
return CGSize(width: view.frame.width - 24, height: 0)
}
}
}
这是我的帖子单元格
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let postImage: UIImageView = {
let view = UIImageView()
view.image = #imageLiteral(resourceName: "one")
return view
}()
func setupViews() {
backgroundColor = .white
addSubview(postImage)
postImage.anchor(top: contentView.topAnchor, leading: nil, bottom: contentView.bottomAnchor, trailing: nil, padding: .init(top: 20, left: 0, bottom: 0, right: 0), size: .init(width: 70, height: 0))
postImage.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
}
}
这是我的HeaderCell
class HeaderCell: UICollectionViewCell {
var expandedRow: Bool!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.init(r: 220, g: 214, b: 214)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let headerImage: UIImageView = {
let view = UIImageView()
view.image = #imageLiteral(resourceName: "facebook")
return view
}()
let headerLabel: UILabel = {
let label = UILabel()
label.text = "Posts By Network"
return label
}()
let downArrowBtn: UIButton = {
let button = UIButton()
button.setImage(#imageLiteral(resourceName: "downarrow"), for: .normal)
button.imageView?.contentMode = .scaleAspectFit
return button
}()
func setupViews() {
addSubview(headerImage)
addSubview(headerLabel)
addSubview(downArrowBtn)
headerImage.anchor(top: contentView.topAnchor, leading: contentView.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 8, left: 12, bottom: 0, right: 0), size: .init(width: 30, height: 30))
headerLabel.anchor(top: headerImage.topAnchor, leading: headerImage.trailingAnchor, bottom: nil, trailing: nil, padding: .init(top: 2, left: 20, bottom: 0, right: 0))
headerLabel.centerYAnchor.constraint(equalTo: headerImage.centerYAnchor).isActive = true
downArrowBtn.anchor(top: headerLabel.topAnchor, leading: headerLabel.trailingAnchor, bottom: nil, trailing: contentView.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 12), size: .init(width: 20, height: 20))
}
}
单击标题时,所有单元格都在扩展,但是我只想扩展和折叠所选部分,我该怎么做。我是新手,过去几周一直在尝试这种方法。 谢谢