在解包可选值tableView.register(forCellReuseIdentifier)时发现nil

时间:2016-10-19 09:26:22

标签: ios swift uitableview

tableView.register(StatusOriginalTableViewCell.self, forCellReuseIdentifier: StatusTableViewCellIdentifier.OriginalCell.rawValue)
tableView.register(StatusForwardTableViewCell.self, forCellReuseIdentifier: StatusTableViewCellIdentifier.ForwardCell.rawValue)

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return statuses?.count ?? 0
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let status = statuses![indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: StatusTableViewCellIdentifier.cellID(status: status), for: indexPath) as! StatusTableViewCell
    cell.status = status
    return cell
}

我在viewDidLoad注册了两个单元格,但我无法找出它为什么会出现以下错误:

  

在展开可选值swift tableView

时意外地发现了nil

enter image description here

enum StatusTableViewCellIdentifier: String {
case OriginalCell = "OriginalCell"
case ForwardCell = "ForwardCell"

static func cellID(status: Status) -> String {
    return status.retweeted_status != nil ? ForwardCell.rawValue : OriginalCell.rawValue
}

}

class StatusTableViewCell:UITableViewCell {

var picWidthCons: Constraint?
var picHeightCons: Constraint?
var picTopCons: Constraint?

var status: Status? {
    didSet {
        topView.status = status
        contentLabel.text = status?.text
        pictureView.status = status?.retweeted_status != nil ? status?.retweeted_status : status

        let size = pictureView.calculateImageSize()
        pictureView.snp.remakeConstraints { (make) in
            make.left.equalTo(contentLabel)
            picTopCons = make.top.equalTo((size.height) == 0 ? 0 : 10).constraint
            picWidthCons = make.width.equalTo(size.width).constraint
            picHeightCons = make.height.equalTo(size.height).constraint
        }
    }
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupUI()
}

func setupUI() {
    contentView.addSubview(topView)
    contentView.addSubview(contentLabel)
    contentView.addSubview(pictureView)
    contentView.addSubview(footerView)
   }

override func layoutSubviews() {
    topView.snp.makeConstraints { (make) in
        make.top.equalToSuperview()
        make.left.equalToSuperview()
        make.width.equalToSuperview()
        make.height.equalTo(60)
    }
    contentLabel.snp.makeConstraints { (make) in
        make.left.equalTo(topView).offset(10)
        make.top.equalTo(topView.snp.bottom).offset(10)
    }

    footerView.snp.makeConstraints { (make) in
        make.top.equalTo(pictureView.snp.bottom).offset(10)
        make.left.equalTo(pictureView).offset(10)
        make.width.equalToSuperview()
        make.height.equalTo(44)
    }
}
private lazy var topView: StatusTableViewTopView = StatusTableViewTopView()
lazy var contentLabel: UILabel = {
        let label = UILabel.createLabel(color: .darkGray, fontSize: 15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 20
        return label
}()
lazy var pictureView: StatusPictureView = StatusPictureView()
lazy var footerView: UIView = UIView()

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

}

class StatusOriginalTableViewCell: StatusTableViewCell {

override func setupUI() {
    super.setupUI()

    pictureView.snp.makeConstraints { (make) in
        make.top.equalTo(picTopCons as! ConstraintRelatableTarget)
        make.left.equalTo(contentLabel)
        make.width.equalTo(picWidthCons as! ConstraintRelatableTarget)
        make.height.equalTo(picHeightCons as! ConstraintRelatableTarget)
    }
}

}

0 个答案:

没有答案