ios - 为什么 nib 文件中的 UITableViewCell(加载并添加到堆栈)在 viewDidAppear 中为零,但在 viewDidLoad 中没问题?

标签 ios uitableview swift3 ios8

我的 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, adding nib reference there makes sure that when the views are drawn(your tables), they are available to locate in cellForRow.

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 from Nib. Hence the nil error.



This link详细讨论了各种 View 生命周期方法及其调用顺序。

关于ios - 为什么 nib 文件中的 UITableViewCell(加载并添加到堆栈)在 viewDidAppear 中为零,但在 viewDidLoad 中没问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45649921/

相关文章:

ios - 容器应用崩溃:Crashlytics需要更多信息

ios - 如何在没有加号按钮的 App Store Connect 上选择构建

ios - iOS7 上错误的 UITableView 分隔符颜色

ios - Swift 3 破坏了我的 Objective C 和 Swift 处理程序/闭包兼容性

ios - UITextView 延迟滚动添加图像

ios - -[UIWebView setMediaPlaybackRequiresUserAction :] affects all instances

ios - 按顺序使用 SDWebImage 加载图像

swift - 根据来自服务器的图像调整imageview大小,然后调整其他标签

ios - 如何在iOS swift中隐藏/查看标题 View 表

ios - 在swift 3中将字符串转换为DATE类型