I have a Stack in my ViewController and I want to load a nib file(which contain a tableview) and add it to the stack. when I add it in viewDidLoad, there is no problem but when I put it in viewDidAppear app crash and got this error:
fatal error: unexpectedly found nil while unwrapping an Optional value
There are some UITableViewCell which I return them in cellForRowAt function based on row. It seems nib files are nil when they load and added in viewDidAppear!
Here is my code:
class MyOrdersViewController: UIViewController {
@IBOutlet weak var stack: UIStackView!
override func viewDidLoad() {
    super.viewDidLoad()
    //setCards()
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    setCards()
}
func setCards(){
        let orderCard = NibLoader.loadViewFromNib(name: "MyOrdersView", selfInstance: self, nibType: MyOrdersView.self) as! MyOrdersView   
        stack.addArrangedSubview(orderCard)
}
}
class MyOrdersView: UIView,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var seprator: UITableViewCell!
@IBOutlet weak var date: UILabel!
@IBOutlet weak var firstCell: UITableViewCell!
@IBOutlet weak var code: UILabel!
@IBOutlet weak var secondCell: UITableViewCell!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var thirdCell: UITableViewCell!
@IBOutlet weak var orderStep: UILabel!
@IBOutlet weak var payModel: UILabel!
@IBOutlet weak var cancelBtn: UIButton!
@IBOutlet weak var fourthCell: UITableViewCell!
override func awakeFromNib() {
    tableView.delegate = self
    tableView.dataSource = self
    UIFunctions.setBordersStyle(view: cancelBtn, radius: 5, width: 1, color: UIColor.red)
}
func setValue(order:Order){
    self.date.text = order.order_date
    self.code.text = String(describing: order.order_code ?? 0)
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell=UITableViewCell()
    switch indexPath.row {
    case 0:
        cell = seprator
    case 1:
        cell = firstCell
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    case 2:
        cell = secondCell
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    case 3:
        cell = thirdCell
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
    case 4:
        cell = fourthCell
    default:
        break
    }
    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var height = 0
    switch indexPath.row {
    case 0:
        height = 25
    case 4:
        height = 70
    default:
        height = 50
    }
    return CGFloat(height)
}
}
 
    