Sreekanth G
Sreekanth G

Reputation: 692

How to Pass the array in multiple selected UISwitch tableViewCell rows into next view controller

Pass the array in multiple selected UISwitch tableViewCell rows into next view controller

var tableViewData = ["Some1", "Some2","Some3", "Some4"]
var tableViewBoolValues = [false, false, false, false]

I'm taking 2 arrays for displaying in TableView
MyTableView Code is here :-

//MARK: - TableView
extension CategoryListViewController : UITableViewDelegate, UITableViewDataSource, CategoryListDelegate {
    func didTap(on switchState: Bool, at index: Int) {
        tableViewBoolValues[index] = switchState
        tableview.reloadData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableViewData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryListTableViewCell") as? CategoryListTableViewCell
        cell?.categoryLabel?.text = tableViewData[indexPath.row]
        cell?.switchButton.isOn = tableViewBoolValues[indexPath.row]
        cell?.categoryListDelegate = (self as CategoryListDelegate)
        cell?.tag = indexPath.row
        return (cell)!
    }
}

UITableViewCell Code is here :-

import UIKit

@objc protocol CategoryListDelegate: NSObjectProtocol{
    func didTap(on switchState:Bool, at index: Int)
}

class CategoryListTableViewCell: UITableViewCell {

    weak var categoryListDelegate: CategoryListDelegate?
    var indexPath : IndexPath?
    @IBOutlet weak var categoryLabel: UILabel!
    @IBOutlet weak var switchButton: UISwitch!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func switchButtonTapped(_ sender: UISwitch) {

        if (self.categoryListDelegate?.responds(to: #selector(CategoryListDelegate.didTap(on:at:))))! {
            self.categoryListDelegate?.didTap(on:sender.isOn, at: self.tag)
            if switchButton.tag  == indexPath?.row{    
            }
        }
    }
}

For example i'm clicking two rows tableViewBoolValues and tableViewData in TableViewCell, i need to pass that two selected rows tableViewBoolValues and tableViewData into another ViewController

@IBAction func nextVCButtonTapped(_ sender: Any) {

     let storyBoard : UIStoryboard = UIStoryboard(name: "main", bundle:nil)
     let vc = storyBoard.instantiateViewController(withIdentifier: "AnotherViewController") as! AnotherViewControlle
     let selectedRows = tableview.indexPathsForSelectedRows
    // I got struct here 
     self.navigationController?.pushViewController(vc, animated: true)
}

Thanks in Advance..

Upvotes: 1

Views: 181

Answers (1)

FryAnEgg
FryAnEgg

Reputation: 493

You need to save the switch state of the cell's UISwitch with the data arrays. I would use a single array of dictionaries (or objects) for the table data and add an "isSelected" key to track the UISwitch states. Something like:

    var dataObject1 = ["data":"Some1", "boolValue":false, "isSelected": false] as [String : Any]
    var dataObject2 = ["data":"Some2", "boolValue":false, "isSelected": false] as [String : Any]
    var dataObject3 = ["data":"Some3", "boolValue":false, "isSelected": false] as [String : Any]
    var tableViewData = [dataObject1, dataObject2, dataObject3]

In cellForRow() set the UISwitch state to the "isSelected" key. In switchButtonTapped() set the "isSelected" key to the UISwitch state. In nextVCButtonTapped() create array with dataObjects where "isSelected" = true. Pass this array to newly created VC.

Upvotes: 1

Related Questions