Manjunath C.Kadani
Manjunath C.Kadani

Reputation: 174

How to load tableview dynamically in swift 4

enter image description hereI have used Worm Tab Strip cocoapod to get tabs like android, the name of tabs are from server response, I need n number of view controllers based on the number of tab names.

For testing purpose, I have hardcoded tab names, and for each particular tab name, I have another array of sub names. My question is how do I change the tableview contents so that, I get an exact number of sub names according to the tab names

var tabNames = ["Brands", "Sports","Movies", "Mobile","Games"]
var brandsNames = ["Addidas", "Nike","Puma"]
var sportsName = ["Cricket","Fifa","Hockey","Baseball"]
var moviesName = ["Mission Impossible","Matrix","Avatar","Titanic"]
var mobileNames = ["Nokia","Redmi","Samsung"]
var gameNames = ["FIFA 19","PES 19","WWE 2K19","Max Payne"]

What should i try in

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

   // return fruits.count
    }

And

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", 
for: indexPath) as! Cell1
    //cell.textLabel?.text = fruits[indexPath.row]

    return cell
    }

I need to get for Brands tab, I need brandsNames as tableview contents.

The tableview contents must change according to the tab names.

Upvotes: 0

Views: 2885

Answers (2)

Manjunath C.Kadani
Manjunath C.Kadani

Reputation: 174

enter image description hereI used this enter link description here as a reference So that I can pass ID of particular tab to tableview in viewcontroller and load that Tablview, So now I can dynamically add new tabs and the corresponding Tablview also changes

Upvotes: 0

vadian
vadian

Reputation: 285069

Create a custom struct as data model for example

struct Category {  
    let name : String
    let items : [String]
}

Declare a data source array and an index

var categories = [Category]()
var index = 0

In viewDidLoad populate the data source array and set index of the current index of the UISegmentedControl

categories = [Category(name:"Brands", items: ["Addidas", "Nike","Puma"]),
              Category(name:"Sports", items: ["Cricket","Fifa","Hockey","Baseball"]),
              Category(name:"Movies", items: ["Mission Impossible","Matrix","Avatar","Titanic"]),
              Category(name:"Mobile", items: ["Nokia","Redmi","Samsung"]),
              Category(name:"Games", items: ["FIFA 19","PES 19","WWE 2K19","Max Payne"])]

index = // current index of the segmented control

in the IBAction of the segmented control set the index and reload the table view

@IBAction func categorySelection(_ sender: UISegmentedControl) {
    index = sender.selectedSegmentIndex
    tableView.reloadData()
}

The data source methods are

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return categories[index].items.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! Cell1
    cell.textLabel?.text = categories[index].items[indexPath.row]
    return cell
}

Upvotes: 1

Related Questions