[英]Collection View Grid iOS Swift 2
因此,我有一个工作收集视图,其中包含2列,每列3个项目。 在进入iPhone 5s模拟器之前,网格视图非常有用,然后我的网格视图变成了一个单独的列,而不是2。我读了一堆关于自定义布局的文章,但似乎都没有用。 任何帮助表示赞赏。 这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8)
layout.itemSize = CGSize(width: (screenWidth - 32)/2, height: 175)
var collview = UICollectionView(frame: CGRectMake(0, 0, screenWidth, screenHeight), collectionViewLayout: layout)
[collview.reloadData];
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
let myImage = UIImage(named: pictureArray[indexPath.row])
cell.textView.text = textArray[indexPath.row]
cell.imageView.image = myImage
return cell
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pictureArray.count
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: 175, height: 175)
}
因为您将每个项目的大小设置为CGSize(width: 175, height: 175)
,所以当您更换其他手机时,它将无法正常工作。
只是这样更改UICollectionViewDelegate
将UICollectionViewDelegateFlowLayout
更改为UICollectionViewDelegateFlowLayout
)
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: screenWidth / 3, height: screenWidth / 3)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.