2016-10-08 6 views
0

私は、ストーリーボードなしで、プログラムでswicを使ってuicollectionviewを作成しようとしています。私はそれが複数のuicollectionviewになる必要があります。 これは私のコードどのようにプログラムを使って迅速にuicollectionviewを作成するのですか(ストーリーボードなし)

class jobtypeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource{ 
var collectionview1 = UICollectionView() 
var collectionview2 = UICollectionView() 

override func viewDidLoad() { 
    super.viewDidLoad() 

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

    collectionview1 = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    self.collectionview1.delegate = self 
    self.collectionview1.dataSource = self 
    collectionview1.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    self.view.addSubview(collectionview1) 

    collectionview2 = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
    self.collectionview2.delegate = self 
    self.collectionview2.dataSource = self 
    collectionview2.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    self.view.addSubview(collectionview2) 

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    if collectionView == self.collectionview1 { 
     return 5 
    }else{ 
     return 4 
    } 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if collectionView == self.collectionview1 { 
     let cellA = collectionview1.dequeueReusableCellWithReuseIdentifier("Cell",forIndexPath: indexPath) as UICollectionViewCell 
     return cellA 
    } 

    else { 
     let cellB = collectionview2.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as UICollectionViewCell 
     return cellB 
    } 
} 

}

すべては私がレイアウト、デリゲート、およびデータソースを持っている細かいようだが、私はそれを実行したとき、私はアプリを終了、このメッセージのエラーを得ていますキャッチされていない例外 'NSInvalidArgumentException'のために、理由: 'UICollectionViewは、非nilレイアウトパラメータで初期化する必要があります'。私は何か見落としてますか??

+0

で私は、このためのストーリーボードを作成することを追加するのを忘れ、私は純粋なコーディング(ストーリーボードではありませんcollectionview)からuicollectionviewを作りたいです。違いがあるかどうかはわかりません。私はまだsegmentcontrol – Alan

答えて

0

あなたの初期化子が正しく見える、しかし、あなたのメンバ変数は、のviewDidLoadが

var collectionview1 = UICollectionView() 

をトリガーされる前に、あなたは、これらの値を初期化しているので、あなたが暗黙的に開封されたようにそれを変更することができます評価されます空の初期化子で初期化されていますすぐにのviewDidLoad

var collectionview1: UICollectionView! 
+0

でそれを作ることができるため、すべてが赤い笑です。私はそれから "予想されるメンバ名または型名の後のコンストラクタコール"からエラーが発生しました。 – Alan

+0

プロパティを指定した構文に変更してもよろしいですか?エラーから、それはそれのようには聞こえません。代入演算子(=)の代わりにコロンに注意してください。 –

+0

愚かな私、それは今私にエラーを与えませんでしたが、私はviewcontrollerに行くときにすべてが黒になります。 – Alan

関連する問題