简体   繁体   English

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

[英]How to dynamically change the height of a UITableView Cell containing a UICollectionView cell in Swift?

Currently, I have embedded a UICollectionViewCell in a UITableViewCell within one of the sections of my UITableView . 目前,我已经嵌入UICollectionViewCellUITableViewCell我的部分之一内UITableView I know how to dynamically change the cell's height in another section of my UITableView because I have a UITextView in another UITableViewCell that dynamically changes the height of the cell based on how much text is in the UITextView . 我知道如何在UITableView另一部分中动态更改单元格的高度,因为我在另一个UITableViewCell中具有一个UITextView ,该UITextView根据UITextView文本量来动态更改单元格的高度。

The problem I have is in regards to the UITableViewCell containing the UICollectionViewCell . UICollectionViewCell的问题是关于包含UICollectionViewCellUITableViewCell My UICollectionViewCell has one row of 4 images that the user can add via the camera or photo library using a UIImagePickerController . 我的UICollectionViewCell具有一排4张图像,用户可以使用UIImagePickerController通过相机或照片库添加这些图像。

Currently as I have it, when the 5th picture is generated, the UITableViewCell 's height remains static, but the user can scroll horizontally in the UICollectionViewCell like so: 目前,在生成第5张图片时, UITableViewCell的高度保持静态,但是用户可以像这样在UICollectionViewCell水平滚动:

在此处输入图片说明

My end goal is this : 我的最终目标是 在此处输入图片说明

And my storyboard: 而我的故事板:

在此处输入图片说明

Pretty self-explanatory but if there is only 4 images, the UITableViewCell remains the same as in screenshoot 1, but the cell's height will dynamically change if the UICollectionViewCell 's height changes. 不言而喻,但是如果只有4张图像,则UITableViewCell与屏幕快照1相同,但是如果UICollectionViewCell的高度发生变化,则单元格的高度将动态变化。

I have set the UICollectionView 's scroll direction to be vertical only. 我已经将UICollectionView的滚动方向设置为仅垂直 Before explaining further, here's my partial code: 在进一步解释之前,这是我的部分代码:

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)
    }
}

Originally, my indexPath.section == 4 , which contains the UICollectionViewCell returned a height of 95 , but I commented that out and replaced it with returning UITableViewAutomaticDimension . 最初,我的indexPath.section == 4 (其中包含UICollectionViewCell返回的高度为95 ,但我对此进行了注释,并用返回的UITableViewAutomaticDimension替换了它。 I would assume that adjusted the height of the cell to fit the 5th image, but the cell remained a static height even though the UICollectionViewCell ' height changed, allowing me to scroll vertically within that static UITableViewCell height. 我假设已调整单元格的高度以适合第5张图像,但是即使UICollectionViewCell的高度发生了变化,该单元格仍保持静态高度,从而允许我在该静态UITableViewCell高度内垂直滚动。

I know these are some questions I found very similar to my situation, but they didnt help me resolve my particular issue: 我知道这些问题与我的情况非常相似,但它们并没有帮助我解决特定的问题:

  1. Swift: Expand UITableViewCell height depending on the size of the UICollectionView inside it Swift:根据其中的UICollectionView的大小扩展UITableViewCell的高度
  2. Auto Height of UICollectionView inside UITableViewCell UITableViewCell内部的UICollectionView的自动高度
  3. UICollectionView inside a UITableViewCell — dynamic height? UITableViewCell内的UICollectionView —动态高度?

With some of the answers and suggestions, I've added the following: 通过一些答案和建议,我添加了以下内容:

imagesCell.images_ARRAY = images_ARRAY
imagesCell.awakeFromNib()

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

However, this did not have any effects. 但是,这没有任何效果。 Can anyone point me in the right direction on what code I need and placed where? 谁能指出我需要什么代码并将其放置在正确的方向上?

Thanks! 谢谢!

I am using these type of cells in my code, Not performing excellent performance wise(as affecting scrolling smoothness) but will let you achieve required design. 我在我的代码中使用了这些类型的单元格,不是明智地执行出色的性能(因为它会影响滚动的平滑度),但会让您实现所需的设计。

  1. Use CollectionView inside tableViewCell with Vertical ScrollDirection and fixed width(I mean not dynamic in nature). 在tableViewCell内部使用CollectionView并使用Vertical ScrollDirection和固定宽度(我的意思是本质上不是动态的)。 This will put overflowing cells in vertical direction after filling horizontal direction. 填充水平方向后,会将溢出的单元格置于垂直方向。
  2. Take out NSLayoutConstraint from xib(if you are using that) of collectionViewHeight. 从collectionViewHeight的xib(如果正在使用)中取出NSLayoutConstraint。 We will use it in later part. 我们将在后面的部分中使用它。
  3. set UITableViewAutomaticDimension in tableView in heightForRowAtIndexPath method. 在heightForRowAtIndexPath方法的tableView中设置UITableViewAutomaticDimension
  4. And finally set cell's collectionViewHeight while returning cell in cellForRowAtIndexPath method using constraint that we took out in step 2. 最后,使用在步骤2中取出的约束,在cellForRowAtIndexPath方法中返回单元格时,设置单元格的collectionViewHeight。

Here I am attaching some code that may will help: 在这里,我附上一些代码可能会有所帮助:

UITableView Part: 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 Part: UITableViewCell部分:

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

And you will need to reload that particular tableViewCell and reload collectionView inside this tableViewCell so that height function of tableView will be called and height of that tableViewCell will be refreshed, and to handle focused condition of that tableViewCell(when tableViewCell is in focus), I am saying this because if it's not in focus(or say cache, there is difference between them though) then cellForRowAtIndexPath method will be called on scrolling(when this cell is going to come in focus) then tableViewCell height will already be taken care of. 并且您将需要重新加载特定的tableViewCell并在此tableViewCell中重新加载collectionView,以便调用tableView的height函数并刷新该tableViewCell的高度,并处理该tableViewCell的集中条件(当tableViewCell处于焦点时),我之所以这样说是因为,如果它不在焦点上(或者说是缓存,虽然它们之间有区别),那么在滚动时将调用cellForRowAtIndexPath方法(当此单元格将成为焦点时),那么tableViewCell的高度将已经被处理。

Hope this will help to achieve required functionality. 希望这将有助于实现所需的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITableView:如何在单击按钮时动态更改单元格高度? 迅速 - UITableView: How to change cell height dynamically when a button is clicked in it? Swift 如何动态更改UICollectionview单元格高度? - How to change UICollectionview Cell Height dynamically? 如何动态更改UITableView中单元格的高度? - How to dynamically change the height of a cell in a UITableView? 如何在Swift中更改UITableView单元格的高度? - How to change height of UITableView cell in Swift? 如何在UITableView静态单元格中动态更改单元格高度 - How to change cell height dynamically in UITableView static cell 根据单元格不改变单元格高度动态地更改UICollectionView高度 - Change UICollectionView Height Dynamically according to cell's not change cell height 如何在iOS中动态更改UICollectionView单元格高度 - How can change UICollectionView cell height dynamically in iOS 如何根据Swift3中的内容自动更改UICollectionView Cell的高度? - How to change the height of UICollectionView Cell automatically according to content in Swift3? UITableView:如何在单击按钮时动态更改单元格高度? - UITableView: How to change cell height dynamically when a button is clicked in it? 子类的uitableview单元格,用于更改分隔符的高度 - subclass uitableview cell to change separator height swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM