gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 11:05:50 26 4
gpt4 key购买 nike

我正在学习 Swift。我有 2 个问题。
完整来源是 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) Collection View 方法

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 个单元格然后旋转到纵向。)
    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

最佳答案

检查这个问题

Answer detailing how cellForRowAtIndexPath works

上面的答案详细说明了 cellForRowAtIndexPath 如何在 UITableView 中工作

UICollectionView 中的

cellForItemAtIndexPath 是围绕相同的概念构建的。您可以将这两个控件视为 UIScrollView 的优化版本。由于 TableView 或 CollectionView 可能包含多个项目,因此控件会重复使用 View ,并且在需要绘制特定单元格时调用此方法。因此,不要将这些方法视为初始化程序,而是将其视为需要指定单元格外观的绘图方法。

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

关于ios - 旋转时多次调用方法 collectionview(cellForItemAtIndexPath),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740643/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com