自定义uitabelviewcell变量返回nil,IBOutlets工作正常 - Mailcore2

时间:2015-02-26 11:09:29

标签: uitableview swift tableviewcell mailcore2 mailcore

我有这个问题,无论我在自定义uitabelviewcell中放入什么变量“var”,它都会返回nil 而IBOulets工作得很好

tableviewcell.swift

import Foundation
import UIKit
class tableViewCell: UITableViewCell  {

@IBOutlet weak var tableViewLabelDate: UILabel!
@IBOutlet weak var tableViewLabelDisplayName: UILabel!
@IBOutlet weak var tableViewLabelSubject: UILabel!
@IBOutlet weak var tableViewTextViewInfo: UITextView!
var messageRenderingOperation: MCOIMAPMessageRenderingOperation!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}

tableview.swift

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell2: tableViewCell = tableView.dequeueReusableCellWithIdentifier(reuseTableViewCellIdentifier, forIndexPath: indexPath) as tableViewCell
            cell2.tag = 0
             let message: MCOIMAPMessage = mailbox?.messages[indexPath.row] as MCOIMAPMessage!
            let uidKey = message.uid
            cell2.tableViewLabelDisplayName?.text = mailbox?.messages[indexPath.row].header.from.displayName
           cell2.tableViewLabelSubject?.text = mailbox?.messages[indexPath.row].header.subject
            println("Got the subject line: \(mailbox?.messages[indexPath.row].header.subject)")

            cell2.messageRenderingOperation = self.imapSession.plainTextBodyRenderingOperationWithMessage(message, folder: "INBOX", stripWhitespace: false)

            cell2.messageRenderingOperation?.start({ (plaintext: String!, error: NSError!) -> Void in   // Crash at this line
                if error != nil{
                    println("ERROR at messageRenderingOperation\(error)")
                }else {
                cell2.tableViewTextViewInfo.text = plaintext
                cell2.messageRenderingOperation = nil
                }
            })

            return cell2

}

enter image description here

它在此行崩溃

cell2.messageRenderingOperation?.start({ (plaintext: String!, error: NSError!) -> Void in   // Crash at this line

更新

我已经尝试了以及

class tableviewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {
var messageRenderingOperation: MCOIMAPMessageRenderingOperation?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
self.messageRenderingOperation = imapSession.plainTextBodyRenderingOperationWithMessage(message, folder: "INBOX")
self.messageRenderingOperation.start({ (plaintext: String!, error: NSError!) -> Void in
                if error != nil{
                    println("ERROR at messageRenderingOperation\(error)")
                }else {
                cell2.tableViewTextViewInfo.text = plaintext
                    cell2.messageRenderingOperation = nil
                }
            })
}

}

更新2

现在如果我使用

var messageRenderingOperation = MCOIMAPMessageRenderingOperation()
然后它不是零,但仍然崩溃。我认为这是“插件”的问题?

1 个答案:

答案 0 :(得分:0)

由于你的var稍后被初始化,你应该将它声明为可选var messageRenderingOperation: MCOIMAPMessageRenderingOperation?,而不是像你当前声明的那样使用force来解包它。此外,如果您使用的是故事板,则可以从nib方法

中清醒地初始化它
class tableViewCell: UITableViewCell  {

    @IBOutlet weak var tableViewLabelDate: UILabel!
    @IBOutlet weak var tableViewLabelDisplayName: UILabel!
    @IBOutlet weak var tableViewLabelSubject: UILabel!
    @IBOutlet weak var tableViewTextViewInfo: UITextView!
    var messageRenderingOperation: MCOIMAPMessageRenderingOperation?

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

   }

   override func awakeFromNib() {
    super.awakeFromNib()

    self.MCOIMAPMessageRenderingOperation = //some initialization
   }
}