简体   繁体   English

使用 collectionView(_:layout:sizeForItemAt:) 调整 UICollectionView 单元格而不调用 reloadData()

[英]Resizing UICollectionView cells with collectionView(_:layout:sizeForItemAt:) without calling reloadData()

I am trying to implement a UICollectionViewCell that includes a UITextView that resizes during runtime as the amount of text a user enters in it increases, as well as moves the cells above it up or down based on the size of the text box.我正在尝试实现一个UICollectionViewCell ,其中包含一个UITextView ,该UITextView在运行时随着用户输入的文本数量的增加而调整大小,并根据文本框的大小向上或向下移动其上方的单元格。 (This UICollectionView is implemented with the UICollectionViewDelegateFlowLayout ) (这个UICollectionView是用UICollectionViewDelegateFlowLayout实现的)

I am able to resize the actual cell during runtime while the user is entering text into the cell through the textViewDidChange() method of the UITextViewDelegate .当用户通过UITextViewDelegatetextViewDidChange()方法将文本输入到单元格时,我能够在运行时调整实际单元格的大小。 However this does not change the position of the other cells, as collectionView(\\_:layout:sizeForItemAt:) is not called immediately.但是,这不会改变其他单元格的位置,因为collectionView(\\_:layout:sizeForItemAt:)不会立即调用。 I am able to trigger collectionView(_:layout:sizeForItemAt:) if I call reloadData() within the UICollectionView class, which repositions the other cells relative to the one of interest, but the problem with this is that this reloads the all the objects, and the keyboard that immediately closes.如果我在UICollectionView类中调用reloadData() ,我能够触发 collectionView(_:layout:sizeForItemAt:),这会相对于感兴趣的单元重新定位其他单元格,但问题在于这会重新加载所有对象,以及立即关闭的键盘。

Is there a way to trigger collectionView(_:layout:sizeForItemAt:) without calling reloadData() ?有没有办法在不调用reloadData()情况下触发collectionView(_:layout:sizeForItemAt:)

Another question that is getting at what I want to know is Resize CollectionView Cell during runtime without reloading but their question is not answered either.我想知道的另一个问题是在运行时调整 CollectionView Cell 的大小而无需重新加载,但他们的问题也没有得到解答。

I'm not sure what your triggering event is for resizing, but the easiest way to do this I can think of would be passing a weak reference to the collectionView to your collectionViewCell when you create it.我不确定您的触发事件用于调整大小,但我能想到的最简单的方法是在创建 collectionView 时将它的weak引用传递给 collectionViewCell。 Make sure it's weak because cells get created and destroyed all the time and you don't want to have a retain cycle.确保它很弱,因为细胞一直在创建和销毁,并且您不希望有一个保留周期。

One approach might be to just use the didSet for bounds of the cell.一种方法可能是仅将didSet用于单元格的bounds I don't think this will be do anything when you initialize the cell, but whenever the bounds of an individual cell changes, you could call your method there.我认为在初始化单元格时这不会做任何事情,但是每当单个单元格的边界发生变化时,您都可以在那里调用您的方法。 I haven't tested this approach, but I think it's a lazy, low-lift, minimal bookkeeping solution.我还没有测试过这种方法,但我认为这是一个懒惰、低提升、最小簿记的解决方案。

Here's the "physics for poets" of what it would look like:这是“诗人物理学”的样子:

class YourCell: UICollectionViewCell {

    weak var collectionView: UICollectionView?

    // Whatever other stuff you need on your cell

    override var bounds: CGRect {
        didSet {
            // Whenever the bounds get set, the collectionView's layout will be invalidated.
            invalidateCollectionViewLayout()
        }
    }

    private func invalidateCollectionViewLayout() {
        guard let collectionView = collectionView else { return }
        collectionView.collectionViewLayout.invalidateLayout()
    }
}

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

相关问题 在UICollectionViewDelegate#collectionView:didSelectItemAtIndexPath中调用UICollectionView#reloadData:隐藏所有单元格 - Calling UICollectionView#reloadData in UICollectionViewDelegate#collectionView:didSelectItemAtIndexPath: hides all cells 在 Swift 4 中调用 collectionView(_ :layout:sizeForItemAt:) - Call collectionView(_ :layout:sizeForItemAt:) in Swift 4 UICollectionView单元格,自动布局和collectionview布局更改 - UICollectionView cells, auto layout and collectionview layout changes uicollectionview-数据自动加载而无需调用reloaddata - uicollectionview - data automatically load without calling reloaddata UICollectionView reloadData鬼单元格 - UICollectionView reloadData ghost cells UICollectionView单元未在reloadData上清除 - UICollectionView Cells not clearing on reloadData collectionView:layout:sizeForItemAt不叫swift 4 iOS 11 - collectionView:layout:sizeForItemAt not called swift 4 iOS 11 删除单元格后使用自定义布局调整UICollectionView的大小 - Resizing UICollectionView with Custom Layout after deleting cells 更新tableView中的特定单元格而不调用reloadData - Update Specific cells in a tableView without calling reloadData 使用自定义布局时未调用 sizeForItemAt 方法 - sizeForItemAt method is not calling when using custom layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM