2017-05-15 5 views
1

コアデータ呼び出しhasImageからの変数に基づいて2番目のセルを登録したいとします。変数が存在するとき、コレクションビューにメディアセルを使用したいときは、通常のセルを使用する必要があります。変数に基づくインデックスパスの項目のUICollectionViewセル

は、もともと私はインデックスパスでセルのために、次を使用:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath : 
    IndexPath) -> UICollectionViewCell{ 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! 
    ShareCell 

    let friend = fetchedResultsController.object(at: indexPath) as! Friend 

    cell.cell = friend.lastMessage 

    return cell 

} 

を上記のコードは完全に正常に動作します。

以下は新しいロジックを作成しようとした私の試みです。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath : 
    IndexPath) -> UICollectionViewCell{ 

    var cellprop: Cell? { 
     didSet{ 

     if cellprop?.hasImage == false { 

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! 
      ShareCell 

      let friend = fetchedResultsController.object(at: indexPath) as! Friend 

      cell.cell = friend.lastMessage 

      return cell 
     } 
     } 
    } 

} 

私が得るエラーは、void関数の予期しない非void戻り値です。

提案がありますか?

編集:

ShareCellは完全に正常に動作し、それはそれはあなたがcellprop変数のセッターを記載しているが、それが呼ばれることはありません通常のcollectionViewCell

class BaseCell: UICollectionViewCell { 

override init(frame: CGRect){ 
    super.init(frame: frame) 
    setupViews() 
} 

required init?(coder aDecoder: NSCoder){ 
    fatalError("init(coder:)has not been implemented") 
} 

func setupViews(){ 
    backgroundColor = UIColor.white 
} 
} 
+0

は '何を意味するのcell.cell'されますか? – D4ttatraya

答えて

0

まず、あなたの質問は混乱しています。

私は、最後のメッセージにイメージがあるかどうかに基づいてメディアセルまたはレギュラーを選択すると仮定しています。その場合は
は、その後、ここでは解決されています

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath : 
    IndexPath) -> UICollectionViewCell{ 

    let friend = fetchedResultsController.object(at: indexPath) as! Friend 
    if friend.lastMessage.hasImage { 
     let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: mediaCellId, for: indexPath) as! 
    MediaCell 
     mediaCell.message = friend.lastMessage 
     return mediaCell 
    } 
    else { 
     let regularCell = collectionView.dequeueReusableCell(withReuseIdentifier: regularCellId, for: indexPath) as! 
    RegularCell 
     regularCell.message = friend.lastMessage 
     return regularCell 
    } 
} 
+0

このソリューションではエラーは発生しません。ただし、コレクションビューのセルは、ALLが通常のセルまたはメディアセルとして返されます。 – Stefan

0

だことからasidesとして定義されてBaseCellとしてサブクラス化されますあなたのコードで。 returnステートメントはその死んだスコープ内にのみあります

0

cellForItemAt indexPath内にリターンセルが必要です。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath : 
    IndexPath) -> UICollectionViewCell{ 
     var cellprop: Cell? 
     if cellprop.hasImage == false { 

      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! 
      ShareCell 

      let friend = fetchedResultsController.object(at: indexPath) as! Friend 

      cell.cell = friend.lastMessage 

      return cell 
     } 
     else{ 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId2, for: indexPath) as! 
      RegulerCell 
      return cell 
     } 


} 
+0

メンバーhasImageにアクセスできません。間違いなくそこにあります。 – Stefan

+0

NSManagedオブジェクトはCellと呼ばれ、プロパティの1つは.hasImageですがアクセスできません。これは、インスタンスメンバーがタイプCellで使用できないことを示しています。 – Stefan

+0

異なるセルタイプを返すために同じ再利用識別子を使用しているため、これはクラッシュします。 –

0

コンパイルエラーは、didSetメソッド内からセルを返すためです。 didSetはvoid関数なので、これを行うことはできません。

このコンテキストでは、実際にdidSetを使用するべきではありません。

ストーリーボードに2つのプロトタイプセルを作成し、固有の再使用識別子を付ける必要があります。

cellForItemAtでは、dequeueReusableCellを使用してこれらのセルの1つを作成し、セルにプロパティを設定してそれを返す必要があります。これは基本的なif-else文ですが、それ以上のことはありません。

if条件はhasImageをチェックする必要があります。つまり、作成しているセルに対応するCore Dataエンティティにアクセスする必要があります。

+0

私はストーリーボードを使用していないため、コアデータエンティティにアクセスする際に問題が発生しています。 – Stefan

関連する問題