2016-07-08 110 views
0

問題是,我有不同的UILabel內容爲每個單元格,我想單元格的寬度固定和高靈活性取決於內容是UILabel高,我試過針對這個問題提出了不同的解決方案,並觀看了幾個教程,但不幸的是,它沒有爲我鍛鍊。收集視圖單元格自動佈局傳遞每個單元格高度

**我有一個視圖控制器和兩個收集意見 ,我只需要其中的一個有一個靈活HIGHT **

extension TextViewController : UICollectionViewDelegateFlowLayout { 

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

    if collectionView == PagesCollectionView 
    { 
    return CGSizeMake(44.0, 44.0) 
    } 
    else { 

     width = UIScreen.mainScreen().bounds.width - CGFloat(14) 

     return CGSizeMake(width , hight) 
    } 


} 

編輯1:

有沒有一種簡單的方法來在桌面視圖中這樣做?

例如: -

override func viewDidLoad() { 

super.viewDidLoad() 

self.tableView.estimatedRowHeight = 80 
self.tableView.rowHeight = UITableViewAutomaticDimension } 
+0

檢查這個鏈接可以幫助你http://stackoverflow.com/questions/ 27285258/collectionview-dynamic-cell-height-swift –

+0

Thx,這是一個很好的方法,但不是我所需要的,我希望高度等於標籤高度。有沒有可能從這個樂趣中訪問cell.label.hight?我試圖訪問它,但沒有弄清楚如何去做。 – user6510240

回答

0

我會假設你要顯示的單元格中的文本

extension TextViewController : UICollectionViewDelegateFlowLayout { 

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

     if collectionView == PagesCollectionView 
     { 
     return CGSizeMake(44.0, 44.0) 
     } 
     else { 

      let width = UIScreen.mainScreen().bounds.width - CGFloat(14) 
      let text = YourDataSource[indexPath.item].text 
      let size = CGSizeMake(width, 800) // just any arbitrary height to make it compile 
      let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin) 
      let estimatedFrame = NSString(string: text).boundingRectWithSize(size, options: options, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(16)], context: nil) 
      return CGSizeMake(width , estimatedFrame.height) 
     } 


    } 
相關問題