2017-05-28 52 views
0

我想在一個UIViewController中創建兩個tableViews。但是當我試圖給UILabel賦值時,出現一個錯誤:致命錯誤:意外地發現零,同時展開一個可選值TableViewCell中的Swift 3.1標籤 - 意外地發現無

我想知道爲什麼,我有幾乎相同的代碼TableViewController與一個tableView和它的工作沒有問題。看起來這些UI標籤在嘗試爲其分配值時未初始化。但不明白如何解決它。

它失敗在這裏:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    var cell: UITableViewCell? 
    if tableView == self.guestsTableView { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell 
     if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) { 
      print(guestsTable.guestName) // works fine, prints the value 
      print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value 
      cell.guestNameLabel.text = guestsTable.guestName 
      cell.openTimeLabel.text = String(describing: guestsTable.openTime) 
      cell.cellDelegate = self 
     } 
    } 
    else if tableView == self.ordersTableView { 
     cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) 
     //to be done 
    } 
    // Configure the cell... 
    return cell! 
} 

這個類的完整代碼:

import UIKit 
import CoreData 

class TableUIViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellWithButtonDelegate { 
    //The following two variables will not be nil because prepare for segue will set them 
    var tableName: String? 
    var table: TablesTable? = nil 

    fileprivate var currentTableSession: TableSessionTable? { 
     get { 
      let tableSessionTable = TableSessionTable() 
      return tableSessionTable.getCurrentTableSession(table: table!) 
     } 
    } 
    fileprivate var guestsTableFetchedResultsController: NSFetchedResultsController<GuestsTable>? 
    fileprivate var ordersTableFetchedResultsController: NSFetchedResultsController<OrdersTable>? 

    @IBOutlet weak var tableNameLabel: UILabel! 
    @IBOutlet weak var tableCapacityLabel: UILabel! 
    @IBOutlet weak var tableCountOfGuestsLabel: UILabel! 
    @IBOutlet weak var tableDescriptionTextView: UITextView! 
    @IBAction func closeTableButtonPressed(_ sender: UIButton) { 
    } 
    @IBOutlet weak var guestsTableView: UITableView! 
    @IBOutlet weak var ordersTableView: UITableView! 

    @IBAction func addGuestButtonPressed(_ sender: UIButton) { 
     let guestsTable = GuestsTable() 
     let tablesTable = TablesTable() 
     let table = Table(tableName: tableName!, tableCapacity: 0, locationX: nil, locationY: nil, tableImage: nil) 
     try? guestsTable.addNewGuest(table: tablesTable.getOrCreateTable(table: table)) 
     updateUI() 
    } 
    @IBAction func addOrderButtonPressed(_ sender: UIButton) { 
    } 




    override func viewDidLoad() { 
     guestsTableView.dataSource = self 
     guestsTableView.delegate = self 
     guestsTableView.register(GuestAtTableTableViewCell.self, forCellReuseIdentifier: "guestCell") 

     ordersTableView.dataSource = self 
     ordersTableView.delegate = self 
     ordersTableView.register(UITableViewCell.self, forCellReuseIdentifier: "orderCell") 
     updateUI() 
    } 

    func didPressButton(table: TablesTable) { 
    } 


    private func updateUI() { 
     let tableView = guestsTableView 
     let context = AppDelegate.viewContext 
     let request : NSFetchRequest<GuestsTable> = GuestsTable.fetchRequest() 
     request.predicate = NSPredicate(format: "table= %@", currentTableSession!) 
     request.sortDescriptors = [NSSortDescriptor(key: "guestName", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))] 
     guestsTableFetchedResultsController = NSFetchedResultsController<GuestsTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
     try? guestsTableFetchedResultsController?.performFetch() 
     tableView?.reloadData() 
    } 

    private func updateUI1() { 
     let tableView = ordersTableView 
     let context = AppDelegate.viewContext 
     let request : NSFetchRequest<OrdersTable> = OrdersTable.fetchRequest() 
     request.sortDescriptors = [NSSortDescriptor(key: "menuItem", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))] 
     ordersTableFetchedResultsController = NSFetchedResultsController<OrdersTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
     try? ordersTableFetchedResultsController?.performFetch() 
     tableView?.reloadData() 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     var cell: UITableViewCell? 
     if tableView == self.guestsTableView { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell 
      if let guestsTable = guestsTableFetchedResultsController?.object(at: indexPath) { 
       print(guestsTable.guestName) // works fine, prints the value 
       print(cell.guestNameLabel.text) //fails here with error fatal error: unexpectedly found nil while unwrapping an Optional value 
       cell.guestNameLabel.text = guestsTable.guestName 
       cell.openTimeLabel.text = String(describing: guestsTable.openTime) 
       cell.cellDelegate = self 
      } 
     } 
     else if tableView == self.ordersTableView { 
      cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) 
      //to be done 
     } 
     // Configure the cell... 
     return cell! 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     if tableView == self.guestsTableView { 
      return guestsTableFetchedResultsController?.sections?.count ?? 1 
     } 
     else if tableView == self.ordersTableView { 
      return ordersTableFetchedResultsController?.sections?.count ?? 1 
     } 
     else {return 1} 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == self.guestsTableView { 
      if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 { 
       return sections[section].numberOfObjects 
      } 
      else { 
       return 0 
      } 
     } 
     else if tableView == self.ordersTableView { 
      if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 { 
       return sections[section].numberOfObjects 
      } 
      else { 
       return 0 
      } 
     } 
     else {return 0} 
    } 

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
     if tableView == self.guestsTableView { 
      if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 { 
       return sections[section].name 
      } 
      else { 
       return nil 
      } 
     } 
     else if tableView == self.ordersTableView { 
      if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 { 
       return sections[section].name 
      } 
      else { 
       return nil 
      } 
     } 
     else {return nil} 

    } 

    func sectionIndexTitles(for tableView: UITableView) -> [String]? { 
     if tableView == guestsTableView { 
      return guestsTableFetchedResultsController?.sectionIndexTitles 
     } 
     else { 
      return ordersTableFetchedResultsController?.sectionIndexTitles 
     } 
    } 

    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int { 
     if tableView == guestsTableView { 
      return guestsTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0 
     } 
     else if tableView == ordersTableView { 
      return ordersTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0 
     } 
     else {return 0} 
    } 
} 

而且UITableViewCell類,全碼:

import UIKit 

class GuestAtTableTableViewCell: UITableViewCell { 
    weak var cellDelegate: CellWithButtonDelegate? 
    @IBOutlet weak var guestNameLabel: UILabel! 
    @IBOutlet weak var openTimeLabel: UILabel! 
    @IBAction func didPressButton(_ sender: UIButton) { 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
} 
+0

嘗試重新連接UILabel的IBOutlet。 –

+0

試過,但沒有幫助 –

回答

0

我猜你有一個XIB你UITableViewCell註冊xib而不是類。 使用以下命令:

guestsTableView.register(UINib.init(nibName: "GuestAtTableTableViewCell", bundle: nil), forCellReuseIdentifier: "guestCell") 

正如你所創建的故事板本身是一個原型細胞,你應該選擇在故事板的細胞,並設置其標識存在。接下來從guestCell的代碼中刪除註冊行。它應該工作

+0

謝謝,但我沒有這個單元的xib。無論如何,我已經試過你的線,並得到:無法加載捆綁NIB。 :( –

+0

你的牢房有班有IBOutlet ??我編輯了我的答案,如果沒有插座 – rameez

+0

是的,請檢查我的文章中的最後一個代碼清單 –

相關問題