swift - 展开和折叠 UICollectionViewController 中选定部分的单元格
问题描述
单击部分标题时,我一直在尝试折叠和展开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))
}
}
单击标题时,所有单元格都在展开,但我只想展开和折叠选定的部分,我该怎么做。我是 swift 新手,过去几周一直在尝试这个。谢谢
解决方案
推荐阅读
- c# - Roslyn CodeFix 具有多个要修复的变体
- python - 使用python查找JSON中的缺失值
- google-apps-script - 在相应标题 Google 表格下的编辑移动行
- regex - 从Python中的中文字符串列中删除空格和标点符号
- python - 根据 Pandas DataFrame 中其他列的条件创建新列
- java - android中jcraft jsch SFTP库的ClassNotFound异常
- android - adb devices 未被识别为已知命令
- xamarin.forms - Xamarin Forms:在默认 Android/iOS 应用程序中打开下载的文件
- c# - Excel 到 PDF 中的列宽
- c# - 访问泛型参数的属性