[英]'instantiated view controller with identifier “” from storyboard “Main”, but didn't get a UITableView' while trying to present view controller modally
[英]Runtime Error message: loadView instantiated view controller with identifier ViewController from storyboard Main,but didn't get a TableView
我正在嘗試開發一個 iOS 應用程序,其中 swift 在 UItableview 中有一個自定義單元格。
1 - 我的控制器的(個人信息)代碼:
import UIKit
struct CellData {
let image: UIImage?
let message: String?
}
class personalinformation: UITableViewController {
var data = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
data = [CellData.init(image: UIImage(named: "sampleimage1"), message: "this is first one"), CellData.init(image: UIImage(named: "sampleimage2"), message: "this is second one"), CellData.init(image: UIImage(named: "sampleimage3"), message: "this is third one"), CellData.init(image: UIImage(named: "sampleimage4"), message: "this is last one")]
self.tableView.register(acustomcell.self, forCellReuseIdentifier: "custom")
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "custom") as! acustomcell
cell.myimage = data[indexPath.row].image
cell.mymessage = data[indexPath.row].message
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
}
2 - 自定義單元格代碼在這里,基本上創建一個簡單的自定義單元格文件(customcell.swift):
import UIKit
class acustomcell: UITableViewCell {
var mymessage: String?
var myimage: UIImage?
var messageView: UITextView = {
var textView = UITextView()
textView.translatesAutoresizingMaskIntoConstraints = false
return textView
}()
var myimageView: UIImageView = {
var imageView = UIImageView()
imageView.adjustsImageSizeForAccessibilityContentSizeCategory = false
return imageView
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(myimageView)
self.addSubview(messageView)
myimageView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
myimageView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
myimageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
myimageView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
messageView.leftAnchor.constraint(equalTo: self.myimageView.rightAnchor).isActive = true
messageView.rightAnchor.constraint(equalTo: self.myimageView.rightAnchor).isActive = true
messageView.bottomAnchor.constraint(equalTo: self.myimageView.bottomAnchor).isActive = true
messageView.topAnchor.constraint(equalTo: self.myimageView.topAnchor).isActive = true
}
override func layoutSubviews() {
super.layoutSubviews()
if let mymessage = mymessage {
messageView.text = mymessage
}
if let myimage = myimage {
myimageView.image = myimage
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
一切看起來都不錯,但是在運行時,當我在模擬器上打開視圖時收到以下錯誤消息(沒有編譯器錯誤):
Thread 1: "-[UITableViewController loadView] instantiated view controller with identifier \"UIViewController-dtk-hv-3QF\" from storyboard \"Main\", but didn't get a UITableView."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.