Krunal Nagvadia
Krunal Nagvadia

Reputation: 1162

Swift : TableView Cell data change with the Button Actions

I create the Bus Ticket app. and not i'm setting my Dropping and Boarding point Screen. So I have a list of the data One is For Boarding point and Another is Dropping points in array Type. So I want to setUp As per The following image. When i Select the Boarding Button i want to show the list of the Boarding point and Similar for the Dropping Button.

enter image description here

So i just trying by Following Code But it's Not working for me.

 var arrBoardPoints = [SBDropPickPoint]()  
 var arrDropPoints = [SBDropPickPoint]()


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DroppingCell") as! DroppingCell
        if btnBoarding.isSelected == true {
            cell.lblDrop.text = arrBoardPoints[indexPath.row].title
            cell.lblAddress.text = arrBoardPoints[indexPath.row].address
            cell.lblTime.text = arrBoardPoints[indexPath.row].time
        } else btnDropping.isSelected == true {
            cell.lblDrop.text = arrDropPoints[indexPath.row].title
            cell.lblAddress.text = arrDropPoints[indexPath.row].address
            cell.lblTime.text = arrDropPoints[indexPath.row].time
        }
        return cell
    }

And I reload my tableView Data on the Both Dropping and Boarding Button Action.

Upvotes: 1

Views: 1769

Answers (2)

Abhishek Jadhav
Abhishek Jadhav

Reputation: 706

Follow this step :-

1 - Create UISegmentedController which has two segment index one is Boarding and second is Dropping

2 - Create the third array of your model in your controller

var arrPoints = [SBDropPickPoint]()

3 - Add target method of a segmented controller in viewDidLoad method and assign the first array to arrPoints and mention which segment selected to be first.

    arrPoints = arrBoardPoints
    segmentedController.selectedSegmentIndex = 0

    segmentedController.addTarget(self, action: #selector(segmentedControllerValueChanged(sender:)), for: .valueChanged)

4 - Add code in the target method of a segmented controller which array list you want to display and then reload tableview.

@objc func segmentedControllerValueChanged(sender: UISegmentedControl) {

        switch sender.selectedSegmentIndex {
        case 0:
            arrPoints = arrBoardPoints
        case 1:
            arrPoints = arrDropPoints
        default:
            print("Invalid")
        }

        tableView.reloadData()
    }

5 - In your tableview datasource and delgate method use arrPoints model array to display list.

extension ControllerName: UITableViewDataSource, UITableViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrPoints.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DroppingCell") as! DroppingCell        
        cell.lblDrop.text = arrPoints[indexPath.row].title
        cell.lblAddress.text = arrPoints[indexPath.row].address
        cell.lblTime.text = arrPoints[indexPath.row].time

    return cell

    }


}

Upvotes: 2

Anji Mendpara
Anji Mendpara

Reputation: 90

When you click on btnBoarding then you need to do isSelected = false for dropping button. And ad visa versa if you clicked on btnDropping then then you need to do btnBoarding = false also.

Upvotes: 0

Related Questions