在可展开的集合视图单元格

时间:2016-07-03 08:57:39

标签: ios swift uicollectionview

我有一个包含两个单元格的集合视图。我通过select(didSelectItemAtIndexPath)使单元格展开/折叠。单元格包含其他视图,这些视图应在展开模式下可见并在折叠中隐藏。如果我使用扩展单元格,我的代码可以正常工作,但如果我已经扩展单元格并点击折叠,则展开的单元格会更改其大小,但其他视图仍然可见。也许在gif上看起来很容易理解。 好的方案:

  1. 点击第一个单元格=展开
  2. 再次点击第一个单元格=折叠它并隐藏图片视图
  3. 行李场景:

    1. 点击第一个单元格=展开
    2. 点击第二个单元格=展开第二个单元格&折叠第一个单元格但第一个单元格上的其他图像视图仍然可见
    3. 再次点击第二个单元格=折叠第二个单元格并隐藏其内容。第一个单元格上的附加图像视图仍然可见
    4. enter image description here

      以下是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
      }
      

0 个答案:

没有答案