【发布时间】:2017-11-30 10:33:52
【问题描述】:
我正在初始化一个UICollectionView,如下所示,我想要不同部分的不同行距。
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
var baseCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
baseCollectionView.backgroundColor = UIColor.clearColor()
baseCollectionView.translatesAutoresizingMaskIntoConstraints = false
baseCollectionView.showsVerticalScrollIndicator = false
baseCollectionView.delegate = self
baseCollectionView.dataSource = self
然后定义布局方法如下,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
switch section {
case 0:
return 10.0
case 1:
return 10.0
case 2 :
return 10.0
case 3:
return 10.0
case 4 :
return 0.0
case 5 :
return 0.0
case 6 :
return 0.0
case 7 :
return 0.0
default:
return 0.0
}
}
还有,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
{
switch section {
case 0:
return 10.0
case 1:
return 10.0
case 2 :
return 10.0
case 3:
return 10.0
case 4 :
return 0.0
case 5 :
return 0.0
case 6 :
return 0.0
case 7 :
return 0.0
default:
return 0.0
}
}
还有其他委托,minimumInteritemSpacingForSectionAtIndex 被调用但 minimumLineSpacingForSectionAt 委托方法没有被调用,如何解决这个问题,我哪里错了?
我也遵守了委托协议。
class MyViewController: UIViewController , UICollectionViewDelegateFlowLayout , UICollectionViewDelegate , UICollectionViewDataSource
给你,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
switch section {
case 0:
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 1:
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 2 :
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 3 :
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 4:
return CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)
case 5:
return CGSizeMake(self.contentView.frame.size.width / 2.0,self.contentView.frame.size.height)
case 6:
return CGSizeMake(75.0, self.contentView.frame.size.height)
case 7:
return CGSizeMake(((self.contentView.frame.size.width - 40.0) / 3.0), 150.0)
default:
return CGSizeZero
}
}
对于所有其他人来说,实际上从我的服务器发送的显示设计正在用于设置各种常量。
【问题讨论】:
-
很难相信这是一个 live 代码...从语法上看,它似乎根本无法编译。
-
是什么让你有这种感觉?而且我只会发布我认为有必要获得答案的部分代码,而不是我的整个代码库!
-
@AvinashSharma 最后的评论完全没有必要,也没有任何帮助。仅供参考,如果它们返回相同的东西,您可以将它们组合在一起。您还可以进行模式匹配...
case 0...3: return 10addefault: return 0 -
我已经写了我已经添加了所有需要的方法,如果我不实现该方法,我的单元格将根本不会出现
-
@AvinashSharma,也许您的集合视图中没有足够的单元格,这就是为什么它不需要调用
...minimumLineSpacingForSectionAt:...方法,就好像您没有足够的单元格至少 2行,行距无关紧要,因此系统不关心调用它。
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout