2016-08-20 13 views
2

UICollectionViewを3つ下のセルで作成しようとしています。 これは正常に動作しています。 1つのセルに触れると、このセルが膨張し、他のセルが特定の高さに崩壊するはずです。また、うまく動作します。Swift 3:UICollectionViewCellのサイズを変更すると、プログラムによってオーバーラップします。

私が解決できない問題。私がセルをタップすると、それが望むように展開されます。それはこのものの下に重なる。下のものは、このセルの新しい高さのためのスペースを作るために下に移動しません。

どうすればこの問題を解決できますか?

これは私がこれまでのところ、私はこれを解決したUICollectionViewUICollectionViewCell

/** 
* Tempory 3 items 
*/ 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

/** 
* Defining the cells 
*/ 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! LicenseplateCollectionViewCell 

    cell.backgroundColor = .white 
    cell.layer.cornerRadius = 4 

    if(indexPath.row > 0){ 
     cell.carStack.isHidden = true 
    } 

    return cell 
} 


/** 
* Initial size 
*/ 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    // The first cell always start expanded with an height of 300 
    if(indexPath.row == 0){ 
     return CGSize(width: collectionView.bounds.size.width, height: 300) 
    }else{ 
     // The others start with an height of 80 
     return CGSize(width: collectionView.bounds.size.width, height: 80) 
    } 
} 

/** 
* This functio shoud be handling everything like an accordeon system. 
* When I touch one cell, the others should collapse. 
*/ 
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

    // Animate resize of cell after tapping 
    UIView.animate(withDuration: 0.2, animations: { 
     // For now I have 3 cells, which I will loop 
     for i in 0...2{ 
      // If this is the active cell (I just tapped on) ... 
      if(indexPath.row == i){ 
       let cell = collectionView.cellForItem(at: indexPath) as! LicenseplateCollectionViewCell 
       // This cell is expanded already, collapse it 
       if(cell.frame.size.height > 200){ 
        cell.carStack.isHidden = true 
        cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 80) 
       }else{ 
        // Open up this cell 
        cell.carStack.isHidden = false 
        cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 300) 
       } 
      }else{ 
       // Other cells besides the one I just tapped, will be collapsed 
       print(indexPath.row, indexPath.section) 
       let customPath = IndexPath(row: i, section: 0) 
       print(customPath.row, customPath.section) 
       let cell = collectionView.cellForItem(at: customPath) as! LicenseplateCollectionViewCell 
       cell.carStack.isHidden = true 
       cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 80) 
      } 
     } 
    }) 

} 

答えて

3

に関連したものです。 アニメーション/サイズ変更は、didSelectItemAtオーバーライドでは発生しません。

まず、セルの構成を適切に定義する必要があります。ここでは、キー「アクティブ」にブール値を追加します。 UICollectionViewが示されている前に、私はセルをタップする

、Iは、オブジェクトの配列内trueに「アクティブ」ブール値を切り替え、その後だけcollectionView.reloadData()を呼び出し、これはsizeForItemAtオーバーライドを実行します。これにより、セルが正しく整列され、正確に何をすべきかを確実にします。

sizeForItemAt私はオブジェクト配列内の 'アクティブ'ブール値に基づいて適切な高さを返します。

関連する問題