最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift初始化collectionView=UICollectionView()出错:UICollectionView must be initialized with a non-nil layout parameter

Swift crifan 5719浏览 0评论

代码:

self.collectionView = UICollectionView()

出错:

2015-12-11 20:09:40.730 JianDao[4409:186125] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘UICollectionView must be initialized with a non-nil layout parameter’

搜:

UICollectionView must be initialized with a non-nil layout parameter

参考:

iOS 开发中常见的错误及解决方法 – 简书

ios – UICollectionView: must be initialized with a non-nil layout parameter – Stack Overflow

后来改为:

import UIKit

class PersonInfoViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
   
let totalItemNumInRow:Int = 5
   
   
var memberList:[ContactItem]
   
let curPersonContactItem:ContactItem
   
   
let flowLayout:UICollectionViewFlowLayout

    let collectionView:UICollectionView
   
   
init(personContactItem:ContactItem) {
       
self.curPersonContactItem = personContactItem
       
self.memberList = [ContactItem]()
       
       
self.flowLayout = UICollectionViewFlowLayout()
       
self.flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
       
self.flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20)
       
self.flowLayout.itemSize = CGSizeMake(41, 41)

        self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)

        
        super.init(nibName: nil, bundle: nil)
    }

}

即可。

转载请注明:在路上 » 【已解决】swift初始化collectionView=UICollectionView()出错:UICollectionView must be initialized with a non-nil layout parameter

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.201 seconds, using 22.19MB memory