运行时错误消息:loadView 实例化视图控制器,标识符为 ViewController,来自 Storyboard Main,但未获得 TableView

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."

看这里是拖动通常的 vc 和 table 模板之间的区别 vc(您应该选择选项 1)