Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift:Property';self.tableView';在super.init调用时未初始化_Swift - Fatal编程技术网

Swift:Property';self.tableView';在super.init调用时未初始化

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 =

我的Swift代码中有一个类设计了
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()
    }
}

谢谢你的回答谢谢你的回答