【发布时间】:2021-04-12 09:10:58
【问题描述】:
我正在尝试开发一个在 UItableview 中有一个自定义单元格的 iOS 应用程序。
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 -
请检查您的主情节提要。错误是说它无法从 UITableViewController 获取 tableview。确保将个人信息映射到情节提要中的 UItableViewController
-
是的,我在视图布局中将TableviewController(个人信息)挂钩到了tableview,看起来,是Xcode的问题,我挂钩时没有删除之前的挂钩。我完全删除了视图并添加了一个新视图,然后将所有代码添加回具有相同名称(个人信息)的新 Tableviewcontroller 并再次将其挂钩到 tableview。它奏效了。
标签: ios swift xcode uitableview