2017-01-16 65 views
0

我有一個問題:我有UITableViewUITableViewCell,在單元格我有UICollectionView與不同號碼UICollectionViewCell。我想UITableViewCell高度對應的高度爲UICollectionView。 我看到真正的高度UICollectionView這裏:通過UICollectionView的高度設置UITableViewCell的高度

class TimeViewCell: UITableViewCell { 

     @IBOutlet weak var labelTime: UILabel! 

     var dataForShow = [String]() { 

      didSet{ 
       self.collectionView?.reloadData() 
      } 
     } 

     @IBOutlet weak var collectionView: UICollectionView! 



     override func awakeFromNib() { 
      super.awakeFromNib() 

      collectionView.delegate = self 
      collectionView.dataSource = self 

     } 

    } 

    extension TimeViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

      return dataForShow.count 
     } 


     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

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

      cell.labelCollection.text = dataForShow[indexPath.row] 

      let realValueHeightOfCollection = collectionView.contentSize.height 

//when print realValueHeightOfCollection I see 50.0 (if dataForShow.count <4) or 110.0 (if dataForShow.count >5) 

      return cell 
     } 

    } 

,但如何在高度傳到高度UITableViewCell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell 
    let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour })[indexPath.row] 

    cell.labelTime.text = showHour.showHour() 

    cell.dataForShow = showHour.showFullTime() 


    //here may be set the height of cell? 

    return cell 
} 

UPD故事板:enter image description here

+0

使用'uitableviewautomaticdimension'在'heightOfrow'方法。 –

+0

@the_dahiya_boy在哪裏? –

+0

每個'UITableViewCell'高度是相同還是變量? –

回答

2

我決定了我的問題,通過添加一行:

tableTime.rowHeight = cell.collectionView.collectionViewLayout.collectionViewContentSize.height 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {...}

2

我有一種方法適合你: 我在代碼中使用這個和M你

在TableView中高度方法分享:

// MARK: - TableView Delegate Methods: 
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     var height: CGFloat = 0.0 
     // // //print("arrTA[indexPath.row] = \(arrTA[indexPath.row])") 
     let dict = arrTA[indexPath.row] as! NSDictionary 
     let str = dict[ISSELECTED] as! String 
     let strFlage = dict[FLAGE] as! String 
     let arrFileCount = dict[DATA]!["file_list"] as! NSArray//dict.objectForKey(DATA)?.objectForKey("file_list").count as NSMutableArray 

     if(strFlage == "MA") { 

      height = 50.0 

     } else { 
      height = self.setExpandedViewHeight(arrFileCount.count) + 0.0     
     } 
     return height 
    } 

而我在管理身高時使用了這個功能:

func setExpandedViewHeight(intArrCount: Int) -> CGFloat { 
     var height: CGFloat = 0.0 
     var generatedHeight: Int = 0 

     generatedHeight = intArrCount/3 /// We get number of rows 

     if(intArrCount % 3 == 0) { 

      height = CGFloat(generatedHeight) * 100 
     } else { 

      height = (CGFloat(generatedHeight) + 1) * 100 
     } 

     return height 
    } 

我希望你能讀到並調試你的代碼並執行它。

+0

感謝的,但對於行的每個設備數量可能會有所不同//對於iphone 7也許2 .. –

+0

從這個功能:setExpandedViewHeight我設法行是在所有的設備相同,我希望你永遠不會拒絕這個。 –

1

如果您正在使用自動佈局,那麼你可以試試這個...

self.tableView.estimatedRowHeight = <estimatedRowHeight>; 
self.tableView.rowHeight = UITableViewAutomaticDimension; 

UITableViewAutomaticDimension會工作,如果你設置前置,底部,相對於電池容器視圖頂部約束。

estimatedRowHeight:用可能的行高設置此值。

設置了這些屬性後,您既不需要執行heightForRowAtIndexpath也不需要執行estimatedRowHeight

相關問題