[英]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.