ios - self.tableView.delegate = self swift

标签 ios swift uitableview delegates uicollectionview

如果我有一个 UIViewController 并且我在 Storyboard中将一个 tableView 连接到它,连接 tableview 导出,然后通过连接检查器连接数据源和委托(delegate)方法(cntrl+拖动到 vc 橙色圆圈图标),我仍然需要添加 self.tableView.delegate = selfself.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/

相关文章:

ios - 如何在容器 View 中的 tableview 周围获取可见框架

未安装 iOS TestFlight 共享扩展

swift - 核心数据问题 Swift

ios - 多个UIWindows状态栏冲突

ios - UITableViewRowAction 单元格的圆角半径改变背景颜色

ios - 如何从 UIViewcontroller 的弹出窗口中的 UItableview 中填充数据?

ios - 在最初被拒绝后,我如何获得隐私权限以重新出现?

iphone - 更改应用程序的启动图像,无需重新提交到商店;可能的?

ios - 在UITableViewCell中为detailTextLabel添加右边距

ios - 通过 NSDate 从 NSDictionary 加载 UITableView 部分?