2017-07-18 99 views
0

目前,我在我的UITableView的其中一個部分中嵌入了UICollectionViewCellUITableViewCell。我知道如何在我的UITableView的另一部分動態更改單元格的高度,因爲我在另一個UITableViewCell中有UITextView,它根據UITextView中文本的數量動態更改單元格的高度。如何在Swift中動態更改包含UICollectionView單元格的UITableView單元的高度?

我的問題是關於UITableViewCell包含UICollectionViewCell。我的UICollectionViewCell有一排4張圖像,用戶可以使用UIImagePickerController通過相機或照片庫添加。

目前我有它,當發生第5的圖片,UITableViewCell的高度保持不變,但用戶可以像這樣在UICollectionViewCell水平滾動:

enter image description here

我的最終目標這是enter image description here

而我的故事板:

enter image description here

漂亮的不言自明,但如果只有4張圖像,UITableViewCell與screenshoot 1保持一致,但如果UICollectionViewCell的高度發生變化,則單元格的高度會動態變化。

我已將UICollectionView的滾動方向設置爲vertical。在進一步解釋,這是我的部分代碼:

class TestViewController: UITableViewController, UITextFieldDelegate, UITextViewDelegate, UIImagePickerControllerDelegate 
{ 
    override func viewDidLoad() 
    { 
     super.viewDidLoad() 

     .... 

     tableView.estimatedRowHeight = 40.0 
     tableView.rowHeight = UITableViewAutomaticDimension 

    } 

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

    { 
     var cell: UITableViewCell = UITableViewCell() 

     if indexPath.section == 1 
     { 
      cell = tableView.dequeueReusableCell(withIdentifier: "TextViewCell", for: indexPath) 

      let textView: UITextView = UITextView() 

      textView.isScrollEnabled = false 

      textView.delegate = self 
      cell.contentView.addSubview(textView) 
     } 
     else if indexPath.section == 4 
     { 
      if let imagesCell = tableView.dequeueReusableCell(withIdentifier: "ImagesCell", for: indexPath) as? CustomCollectionViewCell 
      { 
       if images_ARRAY.isEmpty == false 
       { 
        imagesCell.images_ARRAY = images_ARRAY 
        imagesCell.awakeFromNib() 
       } 

       return imagesCell 
      } 

     } 

     return cell 
    } 

    .... 

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
    { 
     if indexPath.section == 1 
     { 
      return UITableViewAutomaticDimension 
     } 
     else if indexPath.section == 4 
     { 
      //return 95.0 
      return UITableViewAutomaticDimension 
     } 

     return 43.0 
    } 

    .... 

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
    { 
     if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage 
     { 
      if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 4)) as? CustomCollectionViewCell 
      { 
       cell.images_ARRAY.append(selectedImage) 
       cell.imagesCollectionView.reloadData() 
      } 
     } 
     picker.dismiss(animated: true, completion: nil) 
    } 

    .... 

    func textViewDidChange(_ textView: UITextView) 
    { 
     ... 
     // Change cell height dynamically 
     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 
} 

class CustomCollectionViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout 
{ 
    @IBOutlet var imagesCollectionView: UICollectionView! 

    var images_ARRAY = [UIImage]() 

    var images = [INSPhotoViewable]() 

    override func awakeFromNib() 
    { 
     super.awakeFromNib() 

     for image in images_ARRAY 
     { 
      images.append(INSPhoto(image: image, thumbnailImage: image)) 
     } 

     imagesCollectionView.dataSource = self 
     imagesCollectionView.delegate = self 
    } 

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

    { 
     return images_ARRAY.count 
    } 

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

    { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! ExampleCollectionViewCell 

     cell.populateWithPhoto(images[(indexPath as NSIndexPath).row] 

     return cell 
    } 

    .... 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets 
    { 
     return UIEdgeInsetsMake(0.0, 25.0, 0.0, 25.0) 
    } 
} 

本來,我indexPath.section == 4,其中包含UICollectionViewCell返回的的高度,但我評論說出來和返回UITableViewAutomaticDimension取而代之。我會假設調整了單元格的高度以適合第5個圖像,但是單元格保持靜態高度,即使UICollectionViewCell的高度發生了變化,也允許我在該靜態高度內垂直滾動。

我知道這些都是一些問題,我發現非常相似,我的情況,但他們沒有幫我解決我的具體問題:

  1. Swift: Expand UITableViewCell height depending on the size of the UICollectionView inside it
  2. Auto Height of UICollectionView inside UITableViewCell
  3. UICollectionView inside a UITableViewCell — dynamic height?

隨着一些答案和建議,我添加了以下內容:

imagesCell.images_ARRAY = images_ARRAY 
imagesCell.awakeFromNib() 

// Added code 
imagesCell.frame = tableView.bounds 
tableView.setNeedsLayout() 
tableView.layoutIfNeeded() 

但是,這並沒有任何效果。任何人都可以在正確的方向上指出我需要什麼代碼並放置在哪裏?

謝謝!

回答

0

我在我的代碼中使用這些類型的單元格,但並不表現出卓越的性能(因爲影響滾動平滑度),但會讓您實現所需的設計。

  1. 在垂直ScrollDirection和固定寬度(我的意思是說,本質上不是動態的)中使用CollectionViewViewViewViewCell。這會在填充水平方向後將垂直方向的細胞溢出。
  2. 從xib中取出NSLayoutConstraint(如果使用的話)collectionViewHeight。我們將在後面的部分中使用它。
  3. set UITableViewAutomaticDimension in tableView in heightForRowAtIndexPath方法。
  4. 最後定格的collectionViewHeight同時使用,我們在一步拿出約束中的cellForRowAtIndexPath方法返回小區2

這裏我附上一些代碼,可能會有所幫助:

的UITableView部分:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: xyzTableViewCell.self), for: indexPath) as! xyzTableViewCell 
    cell.collectionViewHeight.constant = (numberOfCells/5) * cell.cellHeight 
    return cell 
} 

的UITableViewCell部分:

@IBOutlet fileprivate weak var collectionView: UICollectionView! 
@IBOutlet weak var collectionViewHeight: NSLayoutConstraint 

你會需要重新加載特定tableViewCell和重裝的CollectionView這tableViewCell內使的tableView的高度是否函數將被調用,並且tableViewCell的高度將被刷新,以及處理該tableViewCell的聚焦狀態(當tableViewCell我是這樣說的,因爲如果它沒有焦點(或者說緩存,雖然它們之間存在差異),那麼cellForRowAtIndexPath方法將在滾動時被調用(當這個單元格會聚焦時),那麼tableViewCell height將會已經被照顧了。

希望這將有助於實現所需的功能。

相關問題