1

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?

1
  • Please update your question to provide more information about your existing view controller(s?) and the table views. What have you tried? Commented May 9, 2015 at 8:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.