2016-08-20 29 views
2

我正在嘗試創建一個具有3個單元格的UICollectionView。 這是正常工作。當我觸摸一個細胞時,這個細胞應該擴張,其他細胞應該塌陷到一個特定的高度。也工作得很好。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覆蓋。

首先,我必須正確定義帶有單元配置的陣列。這裏我用布爾值添加一個鍵'active'。

當我一個輕按單元,I切換「主動」布爾值true對象陣列內,之後,只需要調用collectionView.reloadData(),這個執行sizeForItemAt倍率已經示出的UICollectionView之前。這確保了電池的正確對齊,並確實做他們應該做的事情。

sizeForItemAt我根據objects數組中的'active'布爾值返回適當的高度。

相關問題