2015-08-03 146 views
0

當我嘗試運行我的應用程序時,它突然顯示類沒有初始化程序的錯誤。告訴我他們是否有任何代碼問題或變量聲明問題。任何人都可以告訴我這個錯誤背後的原因。類ChecklistViewController沒有初始化程序?

import UIKit 
class ChecklistViewController: UITableViewController { 

    var row0item: ChecklistItem 
    var row1item: ChecklistItem 
    var row2item: ChecklistItem 
    var row3item: ChecklistItem 
    var row4item: ChecklistItem 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int 

    { 
     return 5 

    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell 

    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem") 

     let label = cell!.viewWithTag(1000) as!UILabel 



     if indexPath.row == 0 { 

      label.text = row0item.text 

     } else if indexPath.row == 1 { 

      label.text = row1item.text 

     } else if indexPath.row == 2 { 

      label.text = row2item.text 

     } else if indexPath.row == 3 { 

      label.text = row3item.text 

     } else if indexPath.row == 4 { 

      label.text = row4item.text 

     } 

     configureCheckmarkForCell(cell!, indexPath: indexPath) 

     return cell! 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     if let cell = tableView.cellForRowAtIndexPath(indexPath) { 


      if indexPath.row == 0 { 
       row0item.checked = !row0item.checked 

      } else if indexPath.row == 1 { 
       row1item.checked = !row1item.checked 

      } else if indexPath.row == 2 { 
       row2item.checked = !row2item.checked 

      } else if indexPath.row == 3 { 
       row3item.checked = !row3item.checked 
      } else if indexPath.row == 4 { 
       row4item.checked = !row4item.checked 

      } 
      configureCheckmarkForCell(cell, indexPath: indexPath) 

     } 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 

    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 

    } 

    func configureCheckmarkForCell(cell: UITableViewCell, indexPath: NSIndexPath) { 

     var isChecked = false 

     if indexPath.row == 0 { 

      isChecked = row0item.checked 
     } else if indexPath.row == 1 { 

      isChecked = row1item.checked 
     } else if indexPath.row == 2 { 

      isChecked = row2item.checked 
     } else if indexPath.row == 3 { 

      isChecked = row3item.checked 
     } else if indexPath.row == 4 { 

      isChecked = row4item.checked 
     } 

     if isChecked { 

      cell.accessoryType = .Checkmark 
     } else { 

      cell.accessoryType = .None 
     } 
    } 

} 

回答

0

是的,你的變量聲明有問題。 如果你聲明變量不是可選的(如你所做的那樣),你必須在中覆蓋init()來初始化它們。 如果你聲明爲可選的,你不必初始化它們:

var row0item: ChecklistItem? 
var row1item: ChecklistItem? 
var row2item: ChecklistItem? 
var row3item: ChecklistItem? 
var row4item: ChecklistItem? 

訪問某一行項目:

row0item!.aPropertyOrFunction