Home > Mobile >  how to populate tableview based on button press- swift
how to populate tableview based on button press- swift

Time:05-20

enter image description here

I have two buttons scheduled shoots and completed shoots and a table view under it.I can't use segment control here.when I press first button a tableview should fill with a data.when I press second button tableview should populate with different data. how can I achieve this. I'm new to iOS how to solve this?

CodePudding user response:

Make two separate arrays of your data one for Scheduled Shoots and the Other for Completed Shoots Maintain a Boolean Var to switch between them

var scheduledShoots: [YourModel] = [...]
var completedShoots: [YourModel] = [...]
var isScheduledShootSelected = true

Then in your table view methods do this

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return isScheduledShootSelected ? scheduledShoots.count : completedShoots.count
 }

also in your cellForRowAt check on isScheduledShootSelected and populate respective data

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     if isScheduledShootSelected {
        //Populate scheduledShoots array data 
     } else {
        //Populate completedShoots array data 
     }

     return yourCell
   }

Now in your Action Methods

@IBAction func scheduledShootPressed() {
   isScheduledShootSelected = true
   tableView.reloadData()
}

@IBAction func completedShootPressed() {
   isScheduledShootSelected = false
   tableView.reloadData()
}
  • Related