简体   繁体   中英

UITableViewDelegate not working getting an error (Fatal error: Unexpectedly found nil while unwrapping an Optional value in UITableView in swift 4)

The data of namesArray and descriptionArray getting from previous VC.

I have set the delegate and datasource of my tableview but I am getting an below error

Unexpectedly found nil while unwrapping an Optional value

var namesArray: [String] = []
var descriptionArray: [String] = []
@IBOutlet weak var tblProjectList: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    print(namesArray.count)

    self.tblProjectList.delegate = self
    self.tblProjectList.dataSource = self
}

namesArray has data but why at self.tblProjectList.delegate = self app got crashed.

再次删除并创建tableview插座。

Define UITableViewDelegate and UITableViewDataSource in your UIViewController

在此处输入图片说明

Check your IBOutlet is connected to TableView. Sometimes it gets disconnected.

The process to check:

  • Open storyboard and controller side by side and check.
  • In case it is connected, in storyboard check if TableView delegates are connected.
  • Also check the TableView hierarchy. In storyboard it might be out of scope to get connected.

If you are getting

"Unexpectedly found nil while unwrapping an Optional value"

Than there is some issue with cell identifier, array index not set proper. You can check this both scenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM