我在控制器的scrollToItemAtIndexPath
部分使用viewDidLayoutSubviews
,允许我将用户滚动到特定的单元格。
当我们的单元格作为cellForItemAtIndexPath
问题是在cellForItemAtIndexPath
中放置一个打印声明似乎从未被调用过吗?这里发生冲突的原因是什么?解决方案是什么?代码如下:
- (void)viewDidLayoutSubviews {
CDCChannelCollectionView *scrollView;
if (self.view == [self mixMonitorView]) {
scrollView = [[self mixMonitorView] scrollView];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection: self.selectedChanIndex];
[scrollView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CDCChannelStrip *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
if (self.view == self.mixMonitorView) {
NSInteger chanInt = indexPath.section;
NSNumber *chanNum = [NSNumber numberWithInteger:chanInt];
NSNumber *chanNameNumber = [NSNumber numberWithInteger:chanInt + 1];
NSString *chanName = [NSString stringWithFormat:@"CH %@", chanNameNumber];
cell.channelNumber = chanInt;
cell.channelName.text = chanName;
[self getParameters:(chanNum)];
[self.mixMonitorView setChannelsStripToType:(cell)];
cell.clipsToBounds = YES;
return cell;
}
}
答案 0 :(得分:0)
Datasource方法将调用重新加载集合视图。
尝试以下
[yourCollectionViewName reloadData];
答案 1 :(得分:0)
scrollToItemAtIndexPath
不会调用数据源方法,除非它滚动到滚动前不可见的collectionViewCell
。如果滚动到已经可见的单元格,则需要调用reloadData