RomaRobot
RomaRobot

Reputation: 31

Swift. Why doesn't work UITableView delegates?

I try to understand very basic things. I start to work with UITable elements and how to implement it in my code. I find a lot of easy examples with UITable delegates. But when I try to use it in my code with independent ViewController and class, UITableView delegates were never called. And I found that it's necessary to init that delegate in my class. And after that, it have worked. My question, why when I run simple examples with UITableView and without init delegate it works, but in my code I have to init it?

Example code:

    import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var addVideoTextField: UITextField!
    
    var videos: [String] = ["Facebook Interview 3",
                            "iOS Podcasts",
                            "UIButton Animations"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.tableFooterView = UIView(frame: CGRect.zero)
    }
    
}


extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let videoTitle = videos[indexPath.row]
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath)
        cell.textLabel!.text = videoTitle
        
        return cell
    }

}

My code:

import UIKit

class LampsViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField: UITextField!
    @IBOutlet weak var tableView: UITableView!
    
    var videos:[String] = ["Home", "Work", "Office"]
        
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.tableFooterView = UIView(frame: CGRect.zero)
      }
        
      override func viewWillAppear(_ animated: Bool) {
      }

}

extension LampsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let videoTitle = videos[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "LampItem", for: indexPath)
        cell.textLabel!.text = videoTitle
        return cell
    }
    
    
}

Upvotes: 1

Views: 867

Answers (4)

Steven
Steven

Reputation: 93

If you use UITableViewController as subclass, you don't need to use the delegate because It sets the data source and the delegate of the table view to self.

https://developer.apple.com/documentation/uikit/uitableviewcontroller

but if you use TableView as a outlet on your ViewController in this case you must set delegate.

as Jawad wrote, you can set them through the Storyboard or in code. example:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

Upvotes: 0

Jawad Ali
Jawad Ali

Reputation: 14397

In example you provided they connect delegate and datasource of tableView via Storyboard not from code .. thats why it confused you .. you have option .. Either you set them in storyboard or set them in code ...

this is how you can do it in storyboard.. Control + drag to controller .. you will get options to check both .. delegate and dataSource

enter image description here

Upvotes: 0

Noah Iarrobino
Noah Iarrobino

Reputation: 1543

in viewDidLoad you need

tableView.delegate = self
tableView.dataSource = self

Upvotes: 1

Frankenstein
Frankenstein

Reputation: 16341

Assign the tableView's dataSource and delegate to self in the viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
}

Upvotes: 0

Related Questions