iOS iPhone - UICollectionView 在 indexPath 重新加载错误的 UICollectionViewCell
iOS iPhone - UICollectionView reloading wrong UICollectionViewCell at indexPath
我有一个自定义 UICollectionView 子类和自定义 UICollectionViewCells。
问题是当我滚动集合视图时,它会像这样更改项目的位置:
滚动前-
向下滚动然后返回顶部后 -(注意 Modesto 和 Rebecka 如何改变位置)
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];
}
我认为你必须在 MyCustomCollectionViewCell.m
中实现
if (!theObject) {
theObject = obj;
[mediaView setMediaWithObject:obj];
} else {
[mediaView setMediaWithObject:theObject]
}
的假条件
祝你好运
我有一个自定义 UICollectionView 子类和自定义 UICollectionViewCells。 问题是当我滚动集合视图时,它会像这样更改项目的位置:
滚动前-
向下滚动然后返回顶部后 -(注意 Modesto 和 Rebecka 如何改变位置)
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];
}
我认为你必须在 MyCustomCollectionViewCell.m
if (!theObject) {
theObject = obj;
[mediaView setMediaWithObject:obj];
} else {
[mediaView setMediaWithObject:theObject]
}
的假条件
祝你好运