我有一个包含两个单元格的集合视图。我通过select(didSelectItemAtIndexPath)使单元格展开/折叠。单元格包含其他视图,这些视图应在展开模式下可见并在折叠中隐藏。如果我使用扩展单元格,我的代码可以正常工作,但如果我已经扩展单元格并点击折叠,则展开的单元格会更改其大小,但其他视图仍然可见。也许在gif上看起来很容易理解。 好的方案:
行李场景:
以下是cell
代码:
class ExerciseSetCell: BaseCell {
// timer
let timerButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "timer"), forState: .Normal)
button.frame = CGRectMake(0, 0, 25, 25)
button.alpha = 0
return button
}()
override func setupViews() {
super.setupViews()
backgroundColor = UIColor.whiteColor()
addSubview(timerButton)
timerButton.snp_makeConstraints { (make) in
make.width.equalTo(25)
make.height.equalTo(25)
make.bottomMargin.equalTo(-7)
make.centerX.equalTo(self)
}
}
}
这是我的collectionViewController
代码:
class ExerciseDetailVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var exercise: Exercise?
let exerciseSetCell = ExerciseSetCell()
private let setCell = "ExerciseSetCell"
private var expandedCellIndex = -1
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.registerClass(ExerciseSetCell.self, forCellWithReuseIdentifier: setCell)
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(setCell, forIndexPath: indexPath) as! ExerciseSetCell
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! ExerciseSetCell
if expandedCellIndex == indexPath.item {
UIView.animateWithDuration(0.1, animations: {
cell.weightView.alpha = 0
cell.timerButton.alpha = 0
})
expandedCellIndex = -1
} else {
UIView.animateWithDuration(0.25, animations: {
cell.weightView.alpha = 1
cell.timerButton.alpha = 1
})
expandedCellIndex = indexPath.item
}
collectionView.reloadData()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if expandedCellIndex == indexPath.item {
return CGSizeMake(view.frame.width, 200)
}
return CGSizeMake(view.frame.width, 60)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return 2
}