![](/img/trans.png)
[英]Pass the array in multiple selected table cell into next view controller
[英]How to Pass the array in multiple selected UISwitch tableViewCell rows into next view controller
将多个选定 UISwitch tableViewCell 行中的数组传递到下一个视图控制器
var tableViewData = ["Some1", "Some2","Some3", "Some4"]
var tableViewBoolValues = [false, false, false, false]
我要在 TableView 中显示 2 个数组
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{
}
}
}
}
例如,我单击 TableViewCell 中的两行tableViewBoolValues和tableViewData ,我需要将这两个选定的行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)
}
提前致谢..
您需要使用数据数组保存单元格的 UISwitch 的开关状态。 我将为表数据使用单个字典(或对象)数组,并添加一个“isSelected”键来跟踪 UISwitch 状态。 就像是:
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]
在cellForRow()
中将 UISwitch 状态设置为“isSelected”键。 在switchButtonTapped()
将“isSelected”键设置为 UISwitch 状态。 在nextVCButtonTapped()
创建带有 dataObjects 的数组,其中“isSelected”= true。 将此数组传递给新创建的 VC。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.