2016-04-10 46 views
2

我試圖在選中單元格時更改UICollectionView中單元格的高度。收集視圖更改單元格大小

我設法改變高度,但是當我選擇一個單元格時,其他單元格的y位置不會改變,導致選擇的單元格與其他單元格重疊。

如何告訴其他細胞改變y位置細胞高度被修改?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

     if let detailsCell = collectionView.cellForItemAtIndexPath(indexPath) as? UserDetailsCell { 

      UIView.animateWithDuration(0.5, delay: 0, options: .TransitionCurlUp, animations: { 

       detailsCell.frame.size.height = 300 

       }, completion: nil) 

     } 


     print(indexPath.row) 
    } 

回答

1

這裏的問題是,您正在處理單個單元格/項目individualy,您應該只使用autolayout來完成所有工作。

定義單元格/項的大小對委託:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    if let selectedRows = tableView.indexPathsForSelectedRows { 
     if selectedRows.contains(indexPath) { 
      return 300 
     } 
    } 
    return 40 
} 

而在選擇,你剛纔強迫佈局的更新。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.beginUpdates() 
    tableView.endUpdates() 
} 
+0

謝謝!然而在收集視圖委託我count找'heightForRowAtIndexPath' – SNos

+0

如果你不能只使用一個UItableView,你將不得不創建一個UICollectionViewFlowLayout它是更多的工作,我建議如果可能的話遷移到UITableView。 –

+1

檢查此代表:collectionView:layout:sizeForItemAtIndexPath: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/ –

相關問題