Como conectar o dataSource e delegar com o código - iOS Swift
Estou tentando entender melhor como as fontes DataSource e delegadas são conectadas ao UITableView sob o capô quando você faz a conexão através da interface do usuário no Xcode, arrastando e soltando o ícone viewController.
eu encontreiesta discussão mas acho que estou perdendo alguma coisa porque não posso fazê-la funcionar.
Aqui está o código que tenho atualmente que funciona bem conectando as saídas através do XCode (arrastando e soltando).
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.
}
}
Tentei remover as conexões de tomada feitas pelo XCode, criei uma tomada para o tableView (myTable
) e adicionou o seguinte código noviewDidLoad
método, mas não funciona, sem erro, apenas não carrega os dados.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTable.delegate = self
myTable.dataSource = self
}
Alguém pode descrever as etapas necessárias para fazer essa conexão com o código?