【问题标题】:Collectionview Cell Delegate method not called on scrollToItemAtIndexPath?未在 scrollToItemAtIndexPath 上调用 Collectionview Cell Delegate 方法?
【发布时间】:2017-06-29 10:38:43
【问题描述】:

我在控制器的viewDidLayoutSubviews 部分使用scrollToItemAtIndexPath 来允许我将用户滚动到特定单元格。

当它们作为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;
    }
}

【问题讨论】:

  • 您需要滚动到选定的索引吗?
  • 是的,滚动不是问题,它工作正常,事实上它在之后没有正确调用单元格委托方法
  • 是的,因为你打错电话了,像这样NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedChanIndex inSection:0];
  • collectionview 是由部分而不是项目组成的,你的代码什么都不做
  • 你的代码应该在 cellforItemIndexpath 中抛出错误,你还有更多的代码吗??

标签: ios objective-c uicollectionview


【解决方案1】:

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

【讨论】:

  • 它滚动到一个不可见的单元格,水平..所以它应该调用
【解决方案2】:

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

试试下面

[yourCollectionViewName reloadData];

【讨论】:

  • 滚动到特定索引后,您必须调用数据源方法。你试过这个吗?
  • 不,因为它应该在创建可重用单元格时调用方法,就像我手动滚动时一样
猜你喜欢
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
  • 1970-01-01
  • 1970-01-01
  • 2020-06-03
  • 2014-09-16
相关资源
最近更新 更多