gpt4 book ai didi

iOS iPhone - UICollectionView 在 indexPath 重新加载错误的 UICollectionViewCell

转载 作者:行者123 更新时间:2023-11-28 21:51:34 27 4
gpt4 key购买 nike

我有一个自定义 UICollectionView 子类和自定义 UICollectionViewCells。问题是当我滚动 Collection View 时,它会像这样更改项目的位置:

滚动前 -

enter image description here

向下滚动然后返回顶部后 -(注意 Modesto 和 Rebecka 如何改变位置)

enter image description here

UICollectionView 子类非常清晰:

#import "MyCustomCollectionViewController.h"

@interface MyCustomCollectionViewController ()

@end

@implementation MyCustomCollectionViewController

static NSString * const reuseIdentifier = @"cell";


-(id)initWithPins:(NSArray*)someArrayOfStuff {

self = [super initWithNibName:@"MyCustomCollectionViewController" bundle:nil];

if (self) {

if (myArray) {
myArray = [[NSMutableArray alloc] initWithArray:someArrayOfStuff];
}


}

return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

[self.collectionView registerClass:[MyCustomCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

[self.collectionView reloadData];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return myArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];


MyCustomObject *obj = (MyCustomObject*)[myArray objectAtIndex:indexPath.row];
[cell setObject:obj];


return cell;

}

#pragma mark <UICollectionViewDelegateFlowLayout>

- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

CGSize cellSize = CGSizeMake(self.view.frame.size.width/2, self.view.frame.size.width/2);

return cellSize;
}

这是我自定义的 UICollectionViewCell

#import "MyCustomCollectionViewCell.h"


@implementation MyCustomCollectionViewCell
@synthesize theObject;
@synthesize mediaView;

- (void)awakeFromNib {

mediaView = [[UIImageView alloc] initWithFrame:self.contentView.frame];
mediaView.backgroundColor = self.backgroundColor;
[self.contentView addSubview:mediaView];

}

-(id)initWithFrame:(CGRect)frame {

NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCollectionViewCell" owner:nil options:nil];
self = [topLevelObjects objectAtIndex:0];

if (self) {

self.frame = frame;


}

return self;

}

-(void)prepareForReuse {

[mediaView setMediaWithObject:theObject];

}

-(void)setObject:(MyCustomObject*)obj {

if (!theObject) {
theObject = obj;
[mediaView setMediaWithObject:obj];
}

}

@end

我确定我缺少一些明显的东西,但无法弄清楚是什么。谢谢。

最佳答案

试试这个:

-(void) prepareForReuse {

[super prepareForReuse];
[mediaView setMediaWithObject:nil];
}

关于iOS iPhone - UICollectionView 在 indexPath 重新加载错误的 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28110178/

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