Передача массива в нескольких выбранных строках UISwitch tableViewCell в следующий контроллер представления
var tableViewData = ["Some1", "Some2","Some3", "Some4"]
var tableViewBoolValues = [false, false, false, false]
Я беру 2 массива для отображения в TableView
Код MyTableView здесь: -
//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 находится здесь: -
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{
}
}
}
}
Например, я щелкаю две строки tableViewBoolValues и tableViewData в TableViewCell, мне нужно передать эти двавыделенные строки tableViewBoolValues и tableViewData в другой 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)
}
Заранее спасибо ..