collectionView cellForItemAtIndexPath:调用了两次

collectionView cellForItemAtIndexPath: called twice

我有以下代码:-

- (UICollectionViewCell *)collectionView:(UICollectionView       *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{// Dequeue a prototype cell and set the label to indicate the page
CustomCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SurveyCell" forIndexPath:indexPath];
currentQuestion = [finalQuestionArray objectAtIndex:indexPath.row];

return cell;

}

因此,在加载第一个单元格时,cellForItemAtIndexPath 仅被调用一次,但在加载第二个单元格时,cellForItemAtIndexPath 被调用两次,并且 currentQuestion 更新为第三个单元格中的问题,而不是第二个单元格。我该如何解决?

我对集合视图有页面控制。这是创建集合视图并在其上添加分页的代码

-(void)createCollectionView{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flowLayout.minimumLineSpacing = 0;
flowLayout.minimumInteritemSpacing = 0;

// Set up the collection view with no scrollbars, paging enabled
// and the delegate and data source set to this view controller
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];
self.collectionView.showsHorizontalScrollIndicator = YES;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.pagingEnabled = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
// Register a prototype cell class for the collection view
[self.collectionView registerNib:[UINib nibWithNibName:@"CustomCellCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"SurveyCell"];

[self.view addSubview:self.collectionView];
[self createPaging];

}

-(void)createPaging{
// page control
CGFloat w = self.view.frame.size.width;
CGFloat h = self.view.frame.size.height;

// Set up the page control
CGRect frame = CGRectMake(0, h - 84, w, 60);
pageControl = [[UIPageControl alloc] initWithFrame:frame];

// Add a target that will be invoked when the page control is
// changed by tapping on it
[pageControl addTarget:self action:@selector(pageControlChanged:) forControlEvents:UIControlEventValueChanged];

// Set the number of pages to the number of pages in the paged interface
// and let the height flex so that it sits nicely in its frame
pageControl.numberOfPages = numberOfPages;
pageControl.pageIndicatorTintColor = [UIColor blueColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:pageControl];

}

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


    CustomCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SurveyCell" forIndexPath:indexPath];


    for (int i = 0; i < finalQuestionArray.count; i++) {

                NSLog(@"index i %i", i);

                if (indexPath.item == i) {
                    currentQuestion = [finalQuestionArray objectAtIndex:i];


                 }
    }

    return cell;

}

试试这个,也许能解决你的问题。

我们现在应该期望创建一些单元格 (cellForItemAtIndexPath:) 但从未在 iOS 10 中显示。 如果出于某种古怪的原因我们不想要这种美妙的新行为,我们可以将我们的集合视图的 isPrefetchingEnabled 属性 设置为 false(它在 iOS 10 中默认为 true)。

希望这对某人有所帮助! :) 你可以参考下面link [link]