Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS iPhone-UICollectionView在indexPath处重新加载错误的UICollectionViewCell_Ios_Objective C_Iphone_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

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

iOS iPhone-UICollectionView在indexPath处重新加载错误的UICollectionViewCell,ios,objective-c,iphone,uicollectionview,uicollectionviewcell,Ios,Objective C,Iphone,Uicollectionview,Uicollectionviewcell,我有一个自定义UICollectionView子类和自定义UICollectionViewCells。 问题是,当我滚动“收藏”视图时,它会像这样更改项目的位置: #import "MyCustomCollectionViewController.h" @interface MyCustomCollectionViewController () @end @implementation MyCustomCollectionViewController static NSString *

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

#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;
}
卷轴前-

在向下滚动然后返回顶部之后-(注意莫德斯托和丽贝卡是如何改变位置的)

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;
}
我肯定我遗漏了一些明显的东西,但我不知道是什么。谢谢。

试试这个:

-(void) prepareForReuse {

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

我认为您必须实现
false条件
如果(!对象){
对象=对象;
[mediaView设置MediaWithObject:obj];
}否则{
[mediaView设置MediaWithObject:theObject]
}
MyCustomCollectionViewCell.m中的


祝你好运,我还没看完呢。但是你忘了在你的customCell里给[super Prepareforuse]打电话了