collectionView cellForItemAtIndexPath:调用两次

时间:2017-05-22 13:06:53

标签: objective-c uicollectionview cell uicollectionviewcell

我有以下代码: -

- (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,而不是使用第二个单元格。我该如何解决这个问题?

我对集合视图进行了pagecontrol。以下是创建集合视图和在其上添加分页的代码

-(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];

}

2 个答案:

答案 0 :(得分:2)

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

希望这有助于某人! :) 您可以参考以下链接[link] UICollectionView cellForItem called not correctly

答案 1 :(得分:1)

 -(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;

}

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

相关问题