2016-05-22 112 views
-1

我有下一個自定義單元格的UICollectionView:如何自動調整UICollectionViewCell的高度?

enter image description here

現在,我想根據的UITableView的底部

這樣的高度,我的單元格的高度來調整我的UICollectionViewCell高度可以是500,400,800等

我工作了近一個星期,並不能解決這個問題。任何人都可以幫助我嗎?

我已經試過2種方法:

有了:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

並用:

(collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = CGSizeMake(100, 100) 

,但沒有人的作品。任何人都可以幫助我嗎?

+0

儘管如此,'sizeForItemAtIndexPath'是正確的。你不顯示你的實際代碼,所以你一定是做錯了。但沒有人可以幫助你,因爲你已經隱瞞了你在做什麼! – matt

+0

你想看什麼?我會展示它。我不知道你想看到什麼部分,我的代碼很長。所以這就是爲什麼我不在這裏粘貼我的代碼。你能幫我嗎? – Sam

+0

@matt請看看它:http://pastebin.com/hFNvJLXk。我不知道要在'sizeForItemAtIndex'中寫什麼來使其成爲動態的 – Sam

回答

0
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let height = yourTableView.view.frame.height 
    let percentRatio = 0.2 
    return CGSize(width: collectionView.cellForItemAtIndexPath(indexPath)?.frame.width, height: height * percentRatio) 
} 

將它放在ViewController中,該ViewController充當collectionview的數據源和委託。百分比是表格高度的百分比。你可以改變它到任何你喜歡的。希望能幫助到你!

+0

,但我的tableview大小是動態的。它可以是100,500,200等。這就是爲什麼我有問題= / – Sam