2015-02-26 62 views
0

我有這個問題,無論是「變種」我把自定義uitabelviewcell什麼變量,則返回nil 而IBOulets作品就好定製uitabelviewcell變量返回nil,IBOutlets工作正常 - Mailcore2

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 

UPDATE

我已經試過這藏漢

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() 

那麼它不是零,但仍然崩潰。我認爲這是「插件」的問題?

+0

你有沒有嘗試與Obj-C代碼? –

+0

我盡我所能從mailcore2應用程序中的示例複製代碼,相同的結果...我不知道爲什麼它不會工作 – KennyVB

回答

0

由於您的var會在稍後進行初始化,因此您應該將其聲明爲可選var messageRenderingOperation: MCOIMAPMessageRenderingOperation?,而不是像當前聲明的那樣使用強制來展開它。另外,如果你正在使用故事板,你可以初始化裏面的清醒從筆尖方法

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 
    } 
} 
+0

啊,但它仍然返回無 – KennyVB

+0

你可以嘗試初始化你的var內部UITableViewCell awakeFromNib () 方法 ? –

+0

恩,我該怎麼做?從來沒有使用awakeFromNib() – KennyVB