自定义UITableViewCell与选项

时间:2016-07-06 12:21:25

标签: swift uitableview

我正在为tableView创建自定义单元格。我做了一个快速的文件:

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结构中是可选的)时,它会崩溃并显示消息

  

致命错误:在解包可选值时意外发现nil

我理解这条消息,所以我试着制作

@IBOutlet weak var postPhoto: UIImageView?

像一个可选值,但它不起作用,'cos编译器要求我在插入到单元格之前展开值。 附:如果可以提供关于删除imageView的简短建议,当它为零并调整行高以适应时。

2 个答案:

答案 0 :(得分:0)

您不需要触摸您的网点声明,您需要在cellForRowAtIndexPath方法中检查nil(并设置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
}

答案 1 :(得分: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
}

希望这会对你有所帮助。