如果我有一个 UIViewController 并且我在 Storyboard中将一个 tableView 连接到它,连接 tableview 导出,然后通过连接检查器连接数据源和委托(delegate)方法(cntrl+拖动到 vc 橙色圆圈图标),我仍然需要添加 self.tableView.delegate = self
和 self.tableView.datasource = self
到实际的 View Controller ?当然,在实际的 vc 中,我正在实现 tableView 数据/委托(delegate)协议(protocol)。
此外,我假设无论答案是否相同, Collection View Controller 都会以相同的方式通过 Storyboard连接吗?
添加它的优缺点是什么?
class FooController:UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.datasource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { ... }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { ... }
}
最佳答案
do I still need to add self.tableView.delegate = self to the actual view controller?
没有。您通过在 Storyboard 中建立联系来做到这一点。它们完全是同一件事:对 delegate
属性的赋值,以两种不同的方式表示(一种在代码中,一种在 Interface Builder 中“以图形方式”表示)。您只需执行其中一项即可。
但如果您的 View Controller 是一个 TableView Controller (UITableViewController 子类),那么您两者都不,因为 TableView Controller 已经 em> TableView 的数据源和委托(delegate)。 Collection View Controller (UICollectionViewController 子类)也是如此;它已经是 Collection View 的数据源和委托(delegate)。
关于ios - self.tableView.delegate = self swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39442114/