我的 ViewController 中有一个堆栈,我想加载一个 nib 文件(其中包含一个表格 View )并将其添加到堆栈中。当我将它添加到 viewDidLoad
,没有问题,但是当我把它放在viewDidAppear
应用程序崩溃并出现此错误:
fatal error: unexpectedly found nil while unwrapping an Optional value
有一些
UITableViewCell
我在 cellForRowAt
中返回它们基于行的函数。似乎 nib 文件在加载并添加到 viewDidAppear
时为零!这是我的代码:
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)
}
}
最佳答案
The reason seems that since
viewDidLoad
is the first method to be called in the view life cycle, addingnib
reference there makes sure that when the views are drawn(your tables), they are available to locate incellForRow
.But if you put the same code in
viewDidAppear
, the views are already loaded by that time, and your table view is not referenced yet referenced fromNib
. Hence the nil error.
This link详细讨论了各种 View 生命周期方法及其调用顺序。
关于ios - 为什么 nib 文件中的 UITableViewCell(加载并添加到堆栈)在 viewDidAppear 中为零,但在 viewDidLoad 中没问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45649921/