【问题标题】:How to connect dataSource and delegate with code - iOS Swift如何使用代码连接数据源和委托 - iOS Swift
【发布时间】:2016-02-15 03:12:04
【问题描述】:

当您通过 Xcode 中的 UI 通过拖放到 viewController 图标进行连接时,我试图更好地了解 dataSource 和委托出口如何连接到引擎盖下的 UITableView。

我找到了this thread,但我认为我错过了一些东西,因为我无法让它工作。

这是我目前拥有的代码,通过 XCode 连接插座(通过拖放)可以正常工作。

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var hobbies:[String] = ["Computers", "Photography", "Cars", "Reading", "Learning New Things"]


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return hobbies.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text =  hobbies[indexPath.row]

        return cell
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

我尝试删除 XCode 建立的出口连接,为 tableView (myTable) 创建了一个出口,并在 viewDidLoad 方法中添加了以下代码,但它不起作用,没有错误它只是不加载数据。

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

           myTable.delegate = self
           myTable.dataSource = self
        }

有人可以描述用代码进行这种连接所需的步骤吗?

【问题讨论】:

  • 你检查myTable插座连接了吗?
  • 好吧,我实际上是通过将 tableView 拖放到 viewController 来创建插座的。如果正确理解这一点,我上面描述的过程应该可以工作,对吗? Step 1.- Create outlet for tableViewStep 2._ Add assign delegate and dataSource in the viewDidLoad method.DONE
  • 是的,听起来不错。您可能只需要致电reloadData
  • 我会试试看会发生什么。感谢您的确认。
  • 它现在可以工作了,我不确定我错过了什么。非常感谢@Wain。

标签: ios swift uitableview datasource


【解决方案1】:

以编程方式进行连接所需的步骤仅供参考。

1.- 为 tableView 创建 outlet

 @IBOutlet weak var myTable: UITableView!

2.- 在 vi​​ewDidLoad 方法中分配委托和数据源。

myTable.delegate = self
myTable.dataSource = self

3.- 完成

【讨论】:

  • 谢谢,这成功了!我们可以使用故事板来代替编程方式吗?
  • 当然,事实上这可能是最常见的方式。选择您的表格 -> 按住 Control 键将委托和数据源出口拖到 viewController(黄色立方体)。选择表格后,您将在检查器中看到 delegatedataSource 出口。
【解决方案2】:

有几种方法可以做到这一点。

1.最简单的一种是拖放:

  • 在您的 main.storyboard 中选择您的 TableView;
  • 按下控制按钮;
  • 单击鼠标并将鼠标从 TableView 拖动到 ViewController 的图标并放下它;
  • 然后如上图所示选择数据源和委托。

2。另一种方式是通过编码:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()

        tableView.delegate = self
        tableView.dataSource = self
    }
}

PS:确保不要忘记将 tableView 插座连接到代码,方法是将其拖放到 ViewController 类中。

PPS:它还会要求您在您的类中实现以下方法,以便您的 tableView 正常工作:

  • numberOfRowsInSection
  • cellForRowAtIndexPath

对于那些还没有它们的人,你会看到 Xcode 抱怨它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2015-03-20
    相关资源
    最近更新 更多