2016-03-22 28 views
1

配列のセルを表示するUICollectionViewがあります。最初のセルを静的なセルにして、フローを作成して(最終的に新しいセルを追加する)プロンプトとして機能させたい。静的セルをUICollectionViewに追加する

私のアプローチは、コレクションビューに2つのセクションを追加することでしたが、私は現在、cellForItemAtIndexPath内のセルを返す方法を理解できません。これは私の試みです:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if indexPath.section == 0 { 
     let firstCell = collectionView.dequeueReusableCellWithReuseIdentifier("createCell", forIndexPath: indexPath) as! CreateCollectionViewCell 
     firstCell.imageView.backgroundColor = UIColor(white: 0, alpha: 1) 
     return firstCell 
    } else if indexPath.section == 1 { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mainCell", forIndexPath: indexPath) as! MainCollectionViewCell 
     cell.imageView?.image = self.imageArray[indexPath.row] 
     return cell 
    } 
} 

この問題は、私が関数の最後にセルを返さなければならないということです。 if条件の一部として返されないようです。助けてくれてありがとう!

+0

セクションが1であることを確認するのではなく、最後のブロックを通常のelseブロックにします。 – dan

+0

私はいくつかの相違点を持って同様のことを達成しようとしています。私は静的なセルを最後にしたい、私はいくつかのテキストを表示するためにセクションのヘッダーを使用しています。静的なセルに別のセクションを使用すると、それ自体の行に表示されず、他のセル(別のセクションにある)がその下に表示されます。 – Annjawn

答えて

1

Danのコメントを精緻化すると、この関数はUICollectionViewCellのインスタンスを返す必要があります。現時点では、コンパイラはindexPath.sectionが0でも1でもないコードパスを見ることができます。この場合、コードは何も返しません。それがあなたのアプリで論理的に起こることは決してありません。

これを修正する最も簡単な方法は、「else if」を「else」に変更することです。次のように:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    if indexPath.section == 0 { 
     let firstCell = collectionView.dequeueReusableCellWithReuseIdentifier("createCell", forIndexPath: indexPath) as! CreateCollectionViewCell 
     firstCell.imageView.backgroundColor = UIColor(white: 0, alpha: 1) 
     return firstCell 
    } else { // This means indexPath.section == 1 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("mainCell", forIndexPath: indexPath) as! MainCollectionViewCell 
     cell.imageView?.image = self.imageArray[indexPath.row] 
     return cell 
    } 
} 

コードパスが2つしかなく、両方がセルを返すと、コンパイラはより幸せになります。

関連する問題