How to connect dataSource and delegate with code - iOS Swift

故事扮演 提交于 2019-12-04 11:55:52

问题


I'm trying to have a better understanding on how the dataSource and delegate outlets get connected to the UITableView under the hood when you do the connection through the UI in Xcode by dragging and dropping to the viewController icon.

I found this thread but I think I'm missing something because I cannot make it work.

Here is the code I currently have that works fine by connecting the outlets through XCode (by drag and dropping).

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.
    }

}

I tried removing the outlet connections made by XCode, created an outlet for the tableView (myTable) and added the following code in the viewDidLoad method but it doesn't work, no error it just doesn't load the data.

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

           myTable.delegate = self
           myTable.dataSource = self
        }

Can someone describe the steps needed to do this connection with code?


回答1:


Just for reference here are the steps needed to do your connection programmatically.

1.- Create outlet for tableView

 @IBOutlet weak var myTable: UITableView!

2.- Assign delegate and dataSource in the viewDidLoad method.

myTable.delegate = self
myTable.dataSource = self

3.- DONE




回答2:


There are a couple of ways to do it.

1. The simplest one is by dragging and dropping:

  • In your main.storyboard select your TableView;
  • Press your Control button;
  • Click and drag the mouse from your TableView to your ViewController's icon and drop it;
  • Then select dataSource and delegate as shown on the image above.

2. The other way is by coding:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
        super.viewDidLoad()

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

PS: Make sure not to forget connecting your tableView outlet to your code by dragging and dropping it into your ViewController class.

PPS: It'll also ask you to implement the following methods into your class so that your tableView works properly:

  • numberOfRowsInSection
  • cellForRowAtIndexPath

For those who don't have them yet, you'll see Xcode complaining about it.



来源:https://stackoverflow.com/questions/33694415/how-to-connect-datasource-and-delegate-with-code-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!