提问人:emsiloader 提问时间:4/12/2021 更新时间:4/12/2021 访问量:127
运行时错误消息:loadView 实例化视图控制器,标识符为 ViewController 来自情节提要主,但未获取 TableView
Runtime Error message: loadView instantiated view controller with identifier ViewController from storyboard Main,but didn't get a TableView
问:
我正在尝试使用 swift 开发一个 iOS 应用程序,在 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."
评论
UITableViewController
UITableView
view
UIViewController