TableView and its dataSource

0

Just trying to make a simple app, I could not do something which I do not understand because I could not ...

Having the following user interface:

and the following files:

Well, what I try to do is that each tableView in my viewController main, have its own tableViewController which would happen to the delegate and dataSource but it did not even let me link the tableView with its tableViewController

    
asked by MatiEzelQ 25.03.2016 в 00:43
source

1 answer

2

Exactly, as you say in the commentary, these two tables are not Controllers, so you can not associate a controller class with each one. If you want to manipulate and populate those tables you can do it from the main ViewController itself, I'll give you an example:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    // MARK: Propiedades

    @IBOutlet weak var tableView1: UITableView!
    @IBOutlet weak var tableView2: UITableView!

    // Datos para popular las tablas
    let muestraDatos1 = ["Uno", "Dos", "Tres", "Cuatro"]
    let muestraDatos2 = ["Uno", "Dos", "Tres", "Cuatro", "Cinco", "Seis", "Siete"]


    // MARK: Metodos

    override func viewDidLoad() {

        super.viewDidLoad()

        // Puedes hacer la asociacion aqui o en el storyboard
        tableView1.dataSource = self
        tableView1.delegate = self
        tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell1")

        // Puedes hacer la asociacion aqui o en el storyboard
        tableView2.dataSource = self
        tableView2.delegate = self
        tableView2.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell2")

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if tableView == self.tableView1 {
            return muestraDatos1.count
        } else {
            return muestraDatos2.count
        }

    }

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

        var cell: UITableViewCell

        if tableView == self.tableView1 {
            cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath)
            let dato = muestraDatos1[indexPath.row]
            cell.textLabel!.text = dato
        } else {
            cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath)
            let dato = muestraDatos2[indexPath.row]
            cell.textLabel!.text = dato
        }

        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        print("seleccionada fila:      \(indexPath.row)  ")

    }

}
    
answered by 25.03.2016 / 12:03
source