旋转时多次调用方法collectionview(cellForItemAtIndexPath)

时间:2016-06-10 05:38:43

标签: ios swift rotation swift2 uicollectionview

我正在研究斯威夫特。我有两个问题 完整来源是here

1.为什么collectionview(cellForItemAtIndexPath)会这样多次调用?
编辑:我想知道的是为什么每次轮换事件会多次调用collectionview(cellForItemAtIndexPath)。

1)旋转方法

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    print("4 self.collectionView.frame=\(self.collectionView.frame)")
    print("4 size=\(size)")

    self.collectionView.frame.size = size

    let currentSize = self.collectionView.bounds.size
    let offset = CGFloat(self.indexPathRow) * size.height
    print("offset=\(offset)")
    self.collectionView.setContentOffset(CGPointMake(0, offset), animated: true)
    print("self.collectionView.contentOffset=\(self.collectionView.contentOffset)")

    // Suppress the layout errors by invalidating the layout
    self.collectionView.collectionViewLayout.invalidateLayout();


    // Where is the best location of super? Top of this method or bottom?
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
}

2)collectionview方法

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    currentOffset = self.collectionView.contentOffset
    print("currentOffset=\(currentOffset)")

    //print("indexPath=\(indexPath.row)")
    let frame = self.collectionView.frame

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseCellIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell

    let imageName = dataArray[indexPath.row]
    let image = UIImage(named: imageName)
    cell.imageView.image = image
    if (frame.size.width >= frame.size.height) {
        cell.imageView.frame = frame
    }
    else {
        cell.imageView.frame = CGRectMake(0, 0, frame.width*2, frame.height)
    }
    print("cell.imageView.frame=\(frame)")
    print("cell.imageView.image=\(cell.imageView.image!.size)")

    if (indexPath.row % 2 == 0) {
        cell.backgroundColor = UIColor.orangeColor()
    }
    else {
        cell.backgroundColor = UIColor.yellowColor()
    }

    currentIndex = Int(currentOffset.y / frame.size.height)
    print("currentIndex=\(currentIndex)")
    indexPathRow = indexPath.row

    return cell
}
  1. 当我从横向旋转到肖像时,我会收到警告消息。我不想看到这个警告,但我不能。下面是我旋转时的日志消息。此消息显示多个被调用方法。 (您可以满足此日志 - 在横向中传递第12个单元格,然后旋转为纵向。)
  2.     4 self.collectionView.frame=(0.0, 0.0, 667.0, 375.0)
        4 size=(375.0, 667.0)
        2016-06-10 14:22:05.750 CollectionViewWithoutStoryboard[49615:2682527] the behavior of the UICollectionViewFlowLayout is not defined because:
        2016-06-10 14:22:05.750 CollectionViewWithoutStoryboard[49615:2682527] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
        2016-06-10 14:22:05.751 CollectionViewWithoutStoryboard[49615:2682527] The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, 4875}; contentSize: {667, 7500}> collection view layout: .
        2016-06-10 14:22:05.751 CollectionViewWithoutStoryboard[49615:2682527] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
        offset=8671.0
        self.collectionView.contentOffset=(0.0, 4729.0)
        self.collectionView.frame=(0.0, 0.0, 375.0, 667.0)
        currentOffset=(0.0, 4729.0)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=7
        currentOffset=(0.0, 4729.0)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=7
        self.collectionView.frame=(0.0, 0.0, 375.0, 667.0)
        currentOffset=(0.0, 5449.0)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=8
        currentOffset=(0.0, 6029.5)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=9
        currentOffset=(0.0, 6717.0)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=10
        currentOffset=(0.0, 7393.0)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=11
        currentOffset=(0.0, 8218.5)
        cell.imageView.frame=(0.0, 0.0, 375.0, 667.0)
        cell.imageView.image=(1920.0, 804.0)
        currentIndex=12

1 个答案:

答案 0 :(得分:3)

检查此问题

Answer detailing how cellForRowAtIndexPath works

上述答案详细说明了cellForRowAtIndexPath

UITableView的工作原理 cellForItemAtIndexPath中的{p> UICollectionView是围绕相同的概念构建的。您可以将这两个控件都视为UIScrollView的优化版本。由于TableView或CollectionView可能包含多个项目,因此控件会重用视图,并且在绘制特定单元格时,会调用此方法。 因此,不要将这些方法视为初始化器,而应将其视为需要指定单元格外观的绘制方法。

我希望这个解释有所帮助!