Say I create a tableView in navigation controller programmatically. After removing storyboard file and its reference. I put all UI initialization and constraints code into loadView()
as below code.
Running with real device, but the table view is not showed up, and soon this waring pops up.
If I put those code in viewDidLoad
, everything works fine. So, how could I track down this issue? I have searched some similar threads but without fixing my problem.
warning: could not execute support code to read Objective-C class data in the process. This may reduce the quality of type information available.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let table = UITableView()
var pictures = [Picture]()
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
// if put code in loadView() into here, everything works fine.
loadData()
}
override func loadView() {
title = "My Pictures"
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addPicture))
view.addSubview(table)
table.delegate = self
table.dataSource = self
table.register(PictureCell.self, forCellReuseIdentifier: "picture")
table.translatesAutoresizingMaskIntoConstraints = false
table.rowHeight = 120
NSLayoutConstraint.activate([
table.topAnchor.constraint(equalTo: view.topAnchor),
table.leadingAnchor.constraint(equalTo: view.leadingAnchor),
table.trailingAnchor.constraint(equalTo: view.trailingAnchor),
table.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
}
func loadData() {
DispatchQueue.global().async { [weak self] in
if let savedPcitures = self?.defaults.object(forKey: "pictures") as? Data {
let jsonDecoder = JSONDecoder()
do {
self?.pictures = try jsonDecoder.decode([Picture].self, from: savedPcitures)
} catch {
print("Failed to load pictures.")
}
}
DispatchQueue.main.async {
self?.table.reloadData()
}
}
}
@objc func addPicture() {
let picker = UIImagePickerController()
if UIImagePickerController.isSourceTypeAvailable(.camera) {
picker.sourceType = .camera
} else {
fatalError("Camera is not available, please use real device")
}
picker.allowsEditing = true
picker.delegate = self
present(picker, animated: true)
}
...