2016-11-04 5 views
2

iOS開発の初心者です。 私はSwiftを学んできましたが、今日はUICollectionViewControllerを使ってみました。UICollectionViewController Swift 3.0のエラー:Nil以外のレイアウトパラメータで初期化する必要があります

私のコードは次のとおりです。私はそれを実行しようとすると、

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 
    var colView: UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
     layout.itemSize = CGSize(width: 90, height: 120) 

     colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
     colView.dataSource = self 
     colView.delegate = self 
     colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
     colView.backgroundColor = UIColor.white 
     self.view.addSubview(colView) 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 14 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) 
     cell.backgroundColor = UIColor.orange 
     return cell 
    } 


} 

は、しかし、私は次のエラーを取得:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter' 

を私はこの問題を議論する以前の記事を見てきましたが、自分のコードを修正したものは何も見つかりませんでした。

これはどのように修正する必要がありますか?

+0

スタックトレースを貼り付けてください – rounak

答えて

4

ありがとうございました!私はあなたの提案を取り入れました。以下は動作するコードです:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 
let cellId = "CellId" 
var colView: UICollectionView! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let layout = UICollectionViewFlowLayout() 
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
    layout.itemSize = CGSize(width: 111, height: 111) 

    colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    colView.delegate = self 
    colView.dataSource = self 
    colView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    colView.backgroundColor = UIColor.white 

    self.view.addSubview(colView) 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 21 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) 
    cell.backgroundColor = UIColor.orange 
    return cell 
} 

}

は再び、非常に多くの君たちに感謝。すばらしい一日を過ごしてください:D

0

ちょうど1以下のように順序を変更します。

let layout = UICollectionViewFlowLayout() 
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) 
layout.itemSize = CGSize(width: 90, height: 120) 
colView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
self.view.addSubview(colView) 
colView.delegate = self 
colView.dataSource = self 
colView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
colView.backgroundColor = UIColor.white 
0

あなたはこのようなあなたのコードを設定することができます。

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { 

    let cellId = "Cell" 

    lazy var collectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout() 
     // ... layout parameters 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) 
     cv.backgroundColor = .white 
     cv.delegate = self 
     cv.dataSource = self 
     return cv 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.addSubview(collectionView) 
     // ... add your auto layout constraints to the collection view 
     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId) 
    } 

    // Delegate and Data Source Methods... 

} 
1
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let cellSize = CGSize(width: 90, height: 120) 
    // 
    return CGSizeMake(cellSize) 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { 

    return UIEdgeInsetsMake(top: 20, left: 10, bottom: 10, right: 10) 
} 
関連する問題