集合View Grid iOS Swift 2

时间:2016-02-24 02:29:09

标签: ios swift grid uicollectionview uicollectionviewlayout

所以我有一个工作集合视图,每列有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)
}

1 个答案:

答案 0 :(得分:2)

因为您为每个项目设置了UICollectionViewDelegate的大小,所以当您更改其他手机时,它将无法正常工作。

只需更改:(将UICollectionViewDelegateFlowLayout更改为func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: screenWidth / 3, height: screenWidth / 3) }

dyld: Library not loaded Reason: image not found