2017-08-16 7 views
0

私はコレクションビューを持っています。私は各セルをプログラム的に一列にしたいと思っています。これまで私がこれまで行ってきたことは次のとおりです。コレクションセルの行のセルが迅速に処理されますか?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.challenges.count 
    } 

    // make a cell for each cell index path 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     // get a reference to our storyboard cell 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 


     cell.myLabel.text = self.challenges[indexPath.item] 
     cell.backgroundColor = .orange 

     return cell 
    } 


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 

     print("You selected cell #\(indexPath.item)!") 
    } 

} 



class MyCollectionViewCell: UICollectionViewCell { 

    @IBOutlet weak var myLabel: UILabel! 

} 

次に何をすればいいですか?すでにparentViewControllerにcollectionViewのデリゲートを設定するので、次のことを実行することである、これを行う最も簡単な方法を

:あなたは、各セルは、ビューの全体の幅を取るしたいと仮定すると、 enter image description here

答えて

0

関数:func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSizeを返し、幅としてself.view.bound.widthのサイズを返します。

この関数はUICollectionViewDelegateFlowLayoutの一部です。この関数を実装するクラスは、collectionViewのデリゲートだけです。

+0

あなたが上記の機能を見つけることができません。私は次のようにそれを書くことを試みた。しかし、私は文句を言わない、すべてで呼び出されます: FUNCのcollectionView(_ collectionView:UICollectionView、レイアウトcollectionViewLayout:UICollectionViewLayout、sizeForItemAt indexPath:IndexPath) - > CGSize { LETサイズ= CGSize(幅:UIScreen .main.bounds.width、height:10) 戻りサイズ; } –

+0

上記の関数を含むviewcontrollerは 'UICollectionViewDelegateFlowLayout'プロトコルを実装していますか?もしそうなら、コレクションビューのデリゲートとして設定しましたか? – Houwert

関連する問題