self.tableView.delegate =自我Swift [英] self.tableView.delegate = self Swift
问题描述
如果我有UIViewController并在情节提要板上将tableView连接到它,请连接tableview出口,然后通过连接检查器(将cntrl + drag拖动到vc橙色圆圈图标)连接数据源和委托方法,我是否仍然是否需要在实际的视图控制器中添加 self.tableView.delegate = self
和 self.tableView.datasource = self
?当然,在实际的vc中,我正在实现tableView数据/委托协议。
If I have a UIViewController and I hook up a tableView to it in storyboards, connect the tableview outlet, and then connect the datasource and delegate methods via the connections inspector (cntrl+drag to vc orange circle icon), do I still need to add self.tableView.delegate = self
and self.tableView.datasource = self
to the actual view controller? Of course in the actual vc I'm implementing the tableView data/delegate protocols.
此外,我假设对于集合视图控制器来说,答案是一样的通过情节提要板以相同的方式连接?
Also I'm assuming whatever the answer is the same would go for a collection view controller being connected via storyboard the same way?
添加它的利弊是什么?
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 { ... }
}
推荐答案
我仍然需要将self.tableView.delegate = self添加到实际的视图控制器吗?
do I still need to add self.tableView.delegate = self to the actual view controller?
否。您是通过在情节提要板上建立连接来实现的。它们是完全相同的东西:对 delegate
属性的赋值,用两种不同的方式表示(一种在代码中,一种在接口中以图形方式表示)生成器)。
No. You are doing this by making the connection in the storyboard. They are exactly the same thing: an assignment to the delegate
property, expressed in two different ways (one in code, one "graphically" in Interface Builder). You only need to do one of them.
但是,如果您的视图控制器是表视图 controller (UITableViewController子类),那么您会做都不是,因为表视图控制器已经 了表视图的数据源和委托。集合视图 controller (UICollectionViewController子类)也是如此。它已经已经了集合视图的数据源和委托。
But if your view controller were a table view controller (UITableViewController subclass), then you would do neither, because the table view controller is already the table view's data source and delegate. The same is true for a collection view controller (UICollectionViewController subclass); it is already the collection view's data source and delegate.
这篇关于self.tableView.delegate =自我Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!