2016-07-06 2 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 값을 셀에 삽입하기 전에 필요로합니다. 오후 8시 30 분 P.S. 그것이 0 일 때 줄 높이를 맞추기 위해 imageView을 삭제하는 것에 대한 간단한 조언을 줄 수 있다면.

+0

'func tableView (tableView : cellForRowAtIndexPath indexPath :)'는 델리게이트가 아닌 데이터 소스 메소드입니다. – Andrej

답변

0
당신이 (값의 생각과 같은 및 설정 전무) cellForRowAtIndexPath 방법 전무를 확인해야합니다, 당신의 콘센트 선언을 만지지 필요가 없습니다

당신은에 할당하기 전에 객체가 값이 있는지 확인해야
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

IBOutelet. cellForRowAtIndexPath을 다음과 같이 변경해야합니다.

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) 대신 간단한 언 랩핑을 시도하기 때문에 여전히 마음에 들지 않습니다. 생각, 이미 내 대답에 가까운 코드. –

관련 문제