Uitableview TableViewCell中的Swift 3.1标签-意外发现为零

Uitableview TableViewCell中的Swift 3.1标签-意外发现为零,uitableview,swift3,tableviewcell,Uitableview,Swift3,Tableviewcell,我正在尝试在一个UIViewController中创建两个TableView。但当我试图为UILabel赋值时,出现了一个错误:致命错误:在展开可选值时意外地发现了nil 我想知道为什么,我有一个tableView的TableViewController的几乎相同的代码,它可以正常工作。这些UI标签在尝试为其赋值时似乎未初始化。但我不知道如何修复它 它在这里失败了: func tableView(_ tableView: UITableView, cellForRowAt indexPath:

我正在尝试在一个UIViewController中创建两个TableView。但当我试图为UILabel赋值时,出现了一个错误:致命错误:在展开可选值时意外地发现了nil

我想知道为什么,我有一个tableView的TableViewController的几乎相同的代码,它可以正常工作。这些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有一个xib,注册xib而不是类。 使用以下命令:

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

当您在故事板本身中创建了一个原型单元时,您应该在故事板中选择该单元并在那里设置其标识符。接下来,从guestCell代码中删除register行。它应该可以工作

我想您的UITableViewCell有一个xib注册xib而不是类。 使用以下命令:

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

当您在故事板本身中创建了一个原型单元时,您应该在故事板中选择该单元并在那里设置其标识符。接下来,从guestCell代码中删除register行。它应该能用

谢谢,但我没有这个电池的xib。无论如何,我已经试过你的线路了,结果是:无法将笔尖装入包中(你的单元格有类有IBOutlet??我编辑了我的答案,以防没有outletYep,请检查我帖子中的最后一个代码列表。我已经重新检查了你的更新。你能告诉我该怎么做吗?没有收到:(如果没有xib,您能告诉我您在哪里设置了IB插座吗?谢谢,但我没有这个单元的xib。我已经尝试了您的线路,结果得到:无法将NIB装入捆绑包中。)(你的单元格有类有IBOutlet??我编辑了我的答案,以防没有outletYep,请检查我帖子中的最后一个代码列表。我已经重新检查了你的更新。你能告诉我该怎么做吗?没有收到:(如果没有xib,您能告诉我您在哪里设置了IBOutlet吗?请尝试重新连接UILabel的IBOutlet。已尝试,但没有帮助尝试重新连接UILabel的IBOutlet。已尝试,但没有帮助。)