Daniel ORTIZ Daniel ORTIZ - 6 months ago 25
iOS Question

UITable view inside view controller , don't change with delegate and datasource implements

I'm working with swift3 and I need to put a table view inside of view controller, but the table don't change and I don't know the reason :

The delegate and datasource I set and the functions :(

enter image description here

And my code:

import Foundation
import Toast_Swift
public class ViewControllerDataList: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var basededatos: BaseDeDatosFirebase!
private var mensajes: MensajesView!
private var tags: [ListaTags]?
let animals: [String] = ["Horse", "Cow", "Camel", "Sheep", "Goat"]
let cellReuseIdentifier = "cell"
@IBOutlet weak var tablaViewTags: UITableView!
override public func viewDidLoad() {
super.viewDidLoad()
basededatos = BaseDeDatosFirebase()
mensajes = MensajesView(vistaRoot: self)
tags = basededatos.traerTags()
print("Tags actuaes \(tags)")
self.tablaViewTags.delegate=self
self.tablaViewTags.dataSource=self
self.tablaViewTags.register(UITableViewCell.self,
forCellReuseIdentifier: "cell" )


// self.tablaViewTags.register(TagViewController.self,
// forCellReuseIdentifier: "cell")


}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return animals.count
}

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// your cell coding
return UITableViewCell()
}
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (tags?.count)!

}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tablaViewTags.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

// set the text from the data model
cell.textLabel?.text = self.animals[indexPath.row]

return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped cell number \(indexPath.row).")
}


}


But the result is empty and I can't understand where I'm going wrong?

enter image description here

Answer Source

You have to return a number of cells different than 0, if you want some cells to show there:

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0 // your number of cell here
    }

See the line: return 0 // your number of cell here

Try to put a number greater than 0!