【发布时间】:2017-05-22 13:06:53
【问题描述】:
我有以下代码:-
- (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];
}
【问题讨论】:
-
我注意到问题仅在于滚动集合视图单元格。在点击页面控件和调用 pageControlChanged: 方法时很好。
-
您是否考虑过将
-[<UIScrollViewDelegate> scrollViewDidScroll:]实现为UICollectionView是UIScrollView的子类?然后你应该能够使用-[UICollectionView indexPathsForVisibleItems]获取当前单元格并更新页面控件。 -
@Mats 是的,我已经实现了- (void)scrollViewDidScroll:(UIScrollView *)_scrollView{ CGFloat viewWidth = _scrollView.frame.size.width; // 内容偏移量 - 告诉滚动视图滚动了多少。 int pageNumber = floor((_scrollView.contentOffset.x - viewWidth/50) / viewWidth) +1;pageControl.currentPage=pageNumber; }。但问题出在 cellForItemAtIndexPath 中。它被调用了两次,所以得到的问题对象与屏幕上显示的对象不同。是因为滚动视图内容的大小或宽度吗?并创建了两个单元格???
-
我认为你不能期望
-[<UICollectionViewDataSource> collectionView:cellForItemAtIndexPath:]以可预测的方式被调用。特别是如果有动画。设置currentQuestion是个坏主意。最好在-[<UIScrollViewDelegate> scrollViewDidScroll:]中设置currentQuestion。 -
感谢 Mats,这运行良好并且确实解决了问题 :) 所以现在我在 scrollview 委托方法中设置 currentQuestion。但现在你建议如何在第 0 个索引上显示问题?因为 scrollviewDidScroll 只有在滚动到第一个索引单元格后才会被调用。因此它在第 0 个索引中显示为 null
标签: objective-c uicollectionview cell uicollectionviewcell