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
}
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
答案 0 :(得分:3)
检查此问题
Answer detailing how cellForRowAtIndexPath works
上述答案详细说明了cellForRowAtIndexPath
UITableView
的工作原理
cellForItemAtIndexPath
中的{p> UICollectionView
是围绕相同的概念构建的。您可以将这两个控件都视为UIScrollView
的优化版本。由于TableView或CollectionView可能包含多个项目,因此控件会重用视图,并且在绘制特定单元格时,会调用此方法。
因此,不要将这些方法视为初始化器,而应将其视为需要指定单元格外观的绘制方法。
我希望这个解释有所帮助!