Rokon
Rokon

Reputation: 375

Working with multiple UITableViewController in Swift

In my iOS app I have two different UITableView with custom UITableViewCell. These table view will not be displayed at the same time. How can I assign different data source to those using swift?

Upvotes: 1

Views: 173

Answers (2)

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71862

Here is your complete code for that:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var table1: UITableView!
@IBOutlet weak var table2: UITableView!

var table1Data = ["a","b","c"]
var table2Data = ["1","2","3"]

override func viewDidLoad() {
    super.viewDidLoad()
    table2.hidden = true

}

@IBAction func showTable1(sender: AnyObject) {

    table1.hidden = false
    table2.hidden = true
}

@IBAction func showTable2(sender: AnyObject) {

    table1.hidden = true
    table2.hidden = false
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == table1 {
        return table1Data.count
    }else if tableView == table2 {
        return table2Data.count
    }
    return Int()
}

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

    if tableView == table1 {
        let cell = table1.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        let row = indexPath.row
        cell.textLabel?.text = table1Data[row]

        return cell
    }else if tableView == table2 {

        let cell = table2.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! UITableViewCell

        let row = indexPath.row
        cell.textLabel?.text = table2Data[row]

        return cell
    }

    return UITableViewCell()
    }
}

and HERE is your complete working project for more Info.

Upvotes: 2

Viral Savaj
Viral Savaj

Reputation: 3389

You can make outlet for two UITableView in your file

and set UITableViewDataSrouceDelegate as below.

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    if (tableView == Firsttableview) {
        return sectioninFirstTable
    }
    else if (tableView == SecondTableView) {
        return sectioninSecondTable
    }
    return 0
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (tableView == Firsttableview) {
        return rowinSectionForFirstTable
    }
    else if (tableView == SecondTableView) {
        return rowinSectionForSecondTable
    }
    return 0
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (tableView == Firsttableview) {
        var cell : FirstCell = tableView.dequeueReusableCellWithIdentifier("cell") as! ProductWishlistCell

        return cell
    }
    else if (tableView == SecondTableView) {
        var cell : ProductWishlistCell = tableView.dequeueReusableCellWithIdentifier("cell") as! ProductWishlistCell
            return cell
    }
}

Hope this can help you!!

Upvotes: 1

Related Questions