2017-08-06 32 views
0

我在斯威夫特的代碼有一類設計UICollectionViewCells斯威夫特:房產「self.tableView」在super.init調用未初始化

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類,但是這會返回以下錯誤

Property 'self.tableView' not initialized at super.init call 什麼可能是問題,我該如何初始化tableView?

回答

1

您需要可以創建和連接的UITableView出口或創建編程

let tableView = UITableView(frame: yourFrame) 
+0

謝謝你,你回答之前初始化 – sakoaskoaso

1

按照初始化規則所有存儲的屬性必須調用父類的方法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() 
    } 
} 
相關問題