如何在Swift中dynamic更改包含UICollectionView单元格的UITableView单元的高度?

目前,我已经将UICollectionViewCellembedded到UITableViewCell的一个部分中的UITableView 。 我知道如何在UITableView另一部分dynamic地改变单元格的高度,因为我在另一个UITableViewCell中有一个UITextView ,它根据UITextView本的大小来dynamic地改变单元格的高度。

我有的问题是关于包含UICollectionViewCellUITableViewCell 。 我的UICollectionViewCell有一行4个图像,用户可以使用UIImagePickerController通过相机或照片库添加。

目前正如我UICollectionViewCell ,当生成第5张图片时, UITableViewCell的高度保持不变,但用户可以在UICollectionViewCell水平滚动, UICollectionViewCell所示:

在这里输入图像说明

我的最终目标是在这里输入图像说明

而我的故事板:

在这里输入图像说明

相当明显,但如果只有4个图像, UITableViewCell保持与screenshoot 1相同,但是如果UICollectionViewCell的高度改变,单元格的高度将会dynamic改变。

我已经将UICollectionView的滚动方向设置为仅垂直 。 在进一步解释之前,这是我的部分代码:

 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返回了95的高度,但我评论说,并用返回的UITableViewAutomaticDimensionreplace它。 我会假设,调整单元格的高度,以适应第五个图像,但单元格保持静态高度,即使UICollectionViewCell的高度发生了变化,允许我在该静态UITableViewCell高度垂直滚动。

我知道这些是我发现的与我的情况非常相似的一些问题,但他们并没有帮我解决我的问题:

  1. Swift:展开UITableViewCell高度取决于它里面的UICollectionView的大小
  2. UITableViewCell里的UICollectionView的自动高度
  3. UITableViewCell里面的UICollectionView – dynamic高度?

随着一些答案和build议,我已经添加了以下内容:

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

但是,这没有任何影响。 任何人都可以在正确的方向上指出我需要什么代码并放置在哪里?

谢谢!

我在我的代码中使用这些types的单元格,而不是执行优秀的性能明智(影响滚动平滑度),但会让你实现所需的devise。

  1. 在垂直ScrollDirection和固定宽度(我的意思是不dynamic性质)tableViewCell内使用CollectionView。 这将填充水平方向后,将垂直方向溢出细胞。
  2. 从xib中取出NSLayoutConstraint(如果使用的话)的collectionViewHeight。 我们将在后面的部分使用它。
  3. 在tableView的heightForRowAtIndexPath方法中设置UITableViewAutomaticDimension
  4. 最后设置单元格的collectionViewHeight,同时使用我们在步骤2中取出的约束在cellForRowAtIndexPath方法中返回单元格。

在这里,我附上一些可能会有所帮助的代码:

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并重新加载这个tableViewCell里面的collectionView,这样tableView的高度函数将被调用,并且该tableViewCell的高度将被刷新,并处理该tableViewCell的重点条件(当tableViewCell焦点时),我我之所以这样说是因为如果它不在焦点(或者说caching,虽然它们之间存在差异),那么cellForRowAtIndexPath方法将在滚动时调用(当这个单元格将会聚焦),那么tableViewCell的高度已经被处理了。

希望这将有助于实现所需的function。