[英]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
.当用户通过
UITextViewDelegate
的textViewDidChange()
方法将文本输入到单元格时,我能够在运行时调整实际单元格的大小。 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.