2016-07-06 16 views
1

私はtableViewのカスタムセルを作成しています。私は、迅速なファイル作っ:オプション付きカスタムUITableViewCell

import Foundation 
import UIKit 

class CellForPost: UITableViewCell { 
    @IBOutlet weak var postLikes: UILabel! 
    @IBOutlet weak var postText: UILabel! 
    @IBOutlet weak var postDate: UILabel! 
    @IBOutlet weak var postPhoto: UIImageView! 
} 

をし、デリゲートメソッドでそれを実装:ポストは、完全なコンテンツを持っているとき

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! CellForPost 
    cell.postPhoto.image = UIImage.init(data: (posts[indexPath.item].postPhoto)! as NSData) 
    cell.postText.text = posts[indexPath.item].postText 
    cell.postLikes.text = String(posts[indexPath.item].postLikes!) 
    cell.postDate.text = timestampToDate(posts[indexPath.item].postDate!) 
    return cell 
} 

すべてが素晴らしい作品が、例えばpostではオプションです写真はありません(存在しない場合に構造体)は、メッセージ

fatal error: unexpectedly found nil while unwrapping an Optional value

でクラッシュし、私はこのメッセージを理解し、私は

を作ってみました
@IBOutlet weak var postPhoto: UIImageView? 

はオプションの値ですが、動作しません。 'cosコンパイラはunwrapの値をセルに挿入する前に値を返すようにします。 P.P.それがゼロであり、行の高さを合わせるときに、imageViewを削除することについての短いアドバイスを与えることが可能ならば。

+0

func tableView(tableView:cellForRowAtIndexPath indexPath:)はデータソースメソッドであり、デリゲートではありません。 – Andrej

答えて

0
あなたは(値思想として、セットアップゼロ) cellForRowAtIndexPath方法ではnilをチェックする必要がある、あなたの店の宣言に触れ必要はありません

あなたがに割り当てる前に、あなたのオブジェクトが値を持っていることを確認する必要があり
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! CellForPost 

    var image: UIImage? = nil 
    if let imageData = posts[indexPath.item].postPhoto { 
     image = UIImage.init(data: imageData) 
    } 
    cell.postPhoto.image = image 

    cell.postText.text = posts[indexPath.item].postText 

    var postLikes: String? = nil 
    if let likesData = posts[indexPath.item].postLikes { 
     postLikes = String(likesData) 
    } 
    cell.postLikes.text = postLikes 

    var postDate: String? = nil 
    if let dateData = posts[indexPath.item].postDate { 
     postDate = timestampToDate(dateData) 
    } 
    cell.postDate.text = postDate 

    return cell 
} 
+0

エラー:「NSDataからのダウンキャスト?」 "NSData"には、オプションをアンラップするだけです。あなたは "!"を使うつもりでしたか? –

+0

@JohnMurcielagoを 'のように削除しますか?それからNSDataは。あなたのコードからはっきりと分かりません。 –

0

IBOuteletcellForRowAtIndexPathのようなものを変更する必要があります

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("postCell", forIndexPath: indexPath) as! CellForPost 
    if let data = posts[indexPath.item].postPhoto) as? NSData { 
     cell.postPhoto.image = UIImage.init(data: data) 
    } 
    else { 
     cell.postPhoto.image = nil 
    } 
    if let postText = posts[indexPath.item].postText) as? String { 
     cell.postText.text = postText 
    } 
    else { 
     cell.postText.text = "" 
    } 
    if let postLikes = posts[indexPath.item].postLikes) as? Int { 
     cell.postLikes.text = String(postLikes) 
    } 
    else { 
     cell.postLikes.text = "" 
    } 
    if let postDate = posts[indexPath.item].postDate) as? NSDate { 
     cell.postDate.text = timestampToDate(postDate) 
    } 
    else { 
     cell.postDate.text = "" 
    } 
    return cell 
} 

希望すると助かります。

+1

あなたのコードには間違いがあります。一部のフィールドにnilが設定されていると、古い値が上書きされないため、セルには前回のデータがランダムに表示されます。また、それは構文ミスのためにコンパイルされません –

+0

あなたのコードはまだ構文ミスを抱えてコンパイルされません、そして、私はそれを好きではありません、なぜならあなたはデータタイプ(IntとNSDate)私の答えにすでに近いと思いました。 –

関連する問題