iOS - 在 UICollectionView 中动态插入单元格之前如何正确地重新加载单元格
iOS - How to properly reload a cell before inserting it dynamically in UICollectionView
我在 datasource
中动态插入对象时遇到了一种可以理解的行为。
这是我的代码:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[_dataSource insertObject:newObject atIndex:indexPath.row];
[_collectionView insertItemsAtIndexPaths:@[indexPath]];
我在这里做的是在我的 datasource
中插入一个对象(因为这个对象在启动时还没有准备好,我在 delegate
方法中接收它)。
然后,我想在这个特定的 indexPath 触发我的 cellForRow
以创建和插入适当的单元格。
这里的问题是它短暂地显示单元格的 previous content。
即使我在自定义中得到了这个 UICollectionViewCell
class :
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()
中将出口设置为 nil
,因为单元格不会再次从故事板或 NIB 加载。只需重置您的内容(例如文本、图像、控制值)。您可以使用以下经验法则:仅重置您在 cellForRow()
.
中设置的 prepareForReuse()
中的值
当您的出口设置为 nil
时,cellForRow()
无法为视图分配新值,因此您将看到旧值。
我在 datasource
中动态插入对象时遇到了一种可以理解的行为。
这是我的代码:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
[_dataSource insertObject:newObject atIndex:indexPath.row];
[_collectionView insertItemsAtIndexPaths:@[indexPath]];
我在这里做的是在我的 datasource
中插入一个对象(因为这个对象在启动时还没有准备好,我在 delegate
方法中接收它)。
然后,我想在这个特定的 indexPath 触发我的 cellForRow
以创建和插入适当的单元格。
这里的问题是它短暂地显示单元格的 previous content。
即使我在自定义中得到了这个 UICollectionViewCell
class :
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()
中将出口设置为 nil
,因为单元格不会再次从故事板或 NIB 加载。只需重置您的内容(例如文本、图像、控制值)。您可以使用以下经验法则:仅重置您在 cellForRow()
.
prepareForReuse()
中的值
当您的出口设置为 nil
时,cellForRow()
无法为视图分配新值,因此您将看到旧值。