Swift:Property';self.tableView';在super.init调用时未初始化
我的Swift代码中有一个类设计了Swift:Property';self.tableView';在super.init调用时未初始化,swift,Swift,我的Swift代码中有一个类设计了UICollectionViewCells class PostCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource { let tableView: UITableView! override init(frame: CGRect) { super.init(frame: frame) backgroundColor =
UICollectionViewCells
class PostCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
let tableView: UITableView!
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellReuseIdentifier")
tableView.delegate = self
tableView.dataSource = self
designCell()
}
}
我需要在单元格中有一个UITableView
,因此我添加了UITableViewDelegate,UITableViewDataSource
类,但这会返回以下错误
属性“self.tableView”未在super.init调用中初始化
可能存在什么问题以及如何初始化tableView?您需要创建并连接
UITableView的出口,或者以编程方式创建它
let tableView = UITableView(frame: yourFrame)
您需要创建并连接UITableView
的出口,或者以编程方式创建它
let tableView = UITableView(frame: yourFrame)
根据初始化规则,在调用超类的init
方法之前,必须初始化所有存储的属性。将属性声明为隐式展开可选不会初始化该属性
将tableView
声明为非可选,并在调用super
之前对其进行初始化
class PostCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
let tableView: UITableView
override init(frame: CGRect) {
tableView = UITableView(frame: frame)
super.init(frame: frame)
backgroundColor = .white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellReuseIdentifier")
tableView.delegate = self
tableView.dataSource = self
designCell()
}
}
根据初始化规则,在调用超类的init
方法之前,必须初始化所有存储的属性。将属性声明为隐式展开可选不会初始化该属性
将tableView
声明为非可选,并在调用super
之前对其进行初始化
class PostCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
let tableView: UITableView
override init(frame: CGRect) {
tableView = UITableView(frame: frame)
super.init(frame: frame)
backgroundColor = .white
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellReuseIdentifier")
tableView.delegate = self
tableView.dataSource = self
designCell()
}
}
谢谢你的回答谢谢你的回答