gpt4 book ai didi

iOS - 如何在将单元格动态插入到 UICollectionView 之前正确地重新加载单元格

转载 作者:行者123 更新时间:2023-11-29 11:36:49 25 4
gpt4 key购买 nike

在我的 datasource 中动态插入对象时,我遇到了一个可以理解的行为。

这是我的代码:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];

[_dataSource insertObject:newObject atIndex:indexPath.row];
[_collectionView insertItemsAtIndexPaths:@[indexPath]];

我在这里所做的是在我的 datasource 中插入一个对象(因为这个对象在启动时还没有准备好,我在 delegate 方法中接收到它)。

然后,我想在这个特定的 indexPath 触发我的 cellForRow 以创建和插入适当的单元格。

这里的问题是它会短暂显示单元格的上一个 内容

即使我在我的自定义 UICollectionViewCell 类中得到了这个:

override func prepareForReuse() {
super.prepareForReuse()

self.mediaContainer = nil
self.outletTitle.text = ""
self.outletImage.image = nil
self.outletButtonAction = nil
self.bottomView = nil
}

我是否以错误的方式使用了 prepareForReuse

我应该在插入之前调用 reloadItemsAtIndexPath 吗?

这是我的 cellForRow :

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Object *object = [_datasource objectAtIndex:indexPath.row];
UICollectionViewCell *cell = nil;

switch (object.type.intValue) {

case CELLTYPE1... :
// specific cases for specific cell type

.
.
.
case CELLTYPE2:
{
static BOOL nibMyCellloaded = NO;
if (!nibMyCellloaded) {
UINib *nib = [UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"reuseId"];
}

CustomCollectionViewCell* customCell = (CustomCollectionViewCell*)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"reuseId" forIndexPath:indexPath];
[customCell updateCellOutletWith:_customView vc:self];
cell = customCell;
break;



return cell;
}

最佳答案

您应该在 prepareForReuse() 中将 outlets 设置为 nil,因为不会再次从 Storyboard 或 NIB 加载单元格。只需重置您的内容(例如文本、图像、控制值)。您可以使用以下经验法则:仅重置您在 cellForRow() 中设置的 prepareForReuse() 中的值。

当您的导出设置为 nil 时,cellForRow() 无法为 View 分配新值,因此您将看到旧值。

关于iOS - 如何在将单元格动态插入到 UICollectionView 之前正确地重新加载单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48702167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com