在scrollToItemAtIndexPath上没有调用Collectionview Cell Delegate方法?

时间:2017-06-29 10:38:44

标签: ios objective-c uicollectionview

我在控制器的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;
    }
}

2 个答案:

答案 0 :(得分:0)

Datasource方法将调用重新加载集合视图。

尝试以下

[yourCollectionViewName reloadData];

答案 1 :(得分:0)

scrollToItemAtIndexPath不会调用数据源方法,除非它滚动到滚动前不可见的collectionViewCell。如果滚动到已经可见的单元格,则需要调用reloadData