如何将属于UITableView中模型数组对象的数据从数组到表视图?



我有一个模型对象,它有一个数组,我正在按indexPath.row填充数据,而不是我需要将每个部分数组发送到另一个表视图,但它向我发送随机数据如何将特定的选定行数组发送到下一个表视图? 请帮助我理解..

这是我的模型对象:

class ProductModel {
var name:String
var option: [String]
var image:UIImage
init(name:String,option:[String],image:UIImage) {
self.image = image
self.name = name
self.option = option
}
}

这是我的模型对象:

class Data {
static var product = [ProductModel]()
}

这是用于追加数据的数据函数

static func read(compleationHandler:@escaping () -> ()) {
let hospitalList:[String] =  ["A","B,C"]
let hospitalTwo:[String] = ["Sharee bangla","National Park","bangladesh medical"]
let hospitalThree:[String] = ["NSU","MIU","UIU","AIUB"]
let hospitalFour:[String] = ["Dhaka","Comillah","Borials"]
let hospitalFive:[String] = ["iPhone","iPad","iMac"]
DispatchQueue.global(qos: .userInteractive).async {

if Data.product.count == 0 {
Data.product.append(ProductModel(name: "Mahmud", option: hospitalList, image: #imageLiteral(resourceName: "radio")))
Data.product.append(ProductModel(name: "Rangon", option: hospitalTwo, image: #imageLiteral(resourceName: "pause.on.fw")))
Data.product.append(ProductModel(name: "Saikot", option: hospitalThree, image: #imageLiteral(resourceName: "Unit.fw")))
Data.product.append(ProductModel(name: "Ratul", option: hospitalFour, image: #imageLiteral(resourceName: "Region.fw")))
Data.product.append(ProductModel(name: "Jubayer", option: hospitalFive, image: #imageLiteral(resourceName: "add-note.fw")))
}
DispatchQueue.main.async {
compleationHandler()
}
}
}

这是第一个ViewDidLoad和一个用于存储单元的全局数组:

class ModelTableView: UIViewController {
var optionData:[String] = [String]()

@IBOutlet var ModelDataTableVIew: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
ModelDataTableVIew.delegate = self
ModelDataTableVIew.dataSource = self
ModelFunction.read { [weak self] in
self?.ModelDataTableVIew.reloadData()
}
}
}

这是我在标签中创建的cellForRowAtIndex,我添加一个点击手势.当用户单击它时显示另一个表使用此单元格数组数据查看

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row  == Data.product.count {
let cell = ModelDataTableVIew.dequeueReusableCell(withIdentifier: "modelCellBtn") as! ModelCellBtn
return cell
} else {
let cell = ModelDataTableVIew.dequeueReusableCell(withIdentifier: "modelCell") as! ModelCell
cell.configureCell()
optionData = Data.product[indexPath.section].option
let tapGeshture = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
cell.optionLabel.addGestureRecognizer(tapGeshture)
cell.nameImage.image = Data.product[indexPath.row].image
return cell
}

这是准备Segue并将数据发送到下一个视图控制器:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "option") {
let dest = segue.destination as! OptionViewController
dest.data = optionData
}
}

这是我的下一个视图控制器(用于显示选项数据(:

class OptionViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var data:[String] = []
@IBOutlet weak var optionTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
optionTableView.delegate = self
optionTableView.dataSource = self
print(data)
optionTableView.reloadData()
ModelFunction.read { [weak self] in
self?.optionTableView.reloadData()
}
}
}

这里选项视图控制器,在此控制器中,我想在表视图中显示检索到的数组

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = optionTableView.dequeueReusableCell(withIdentifier: "optionCell") as! OptionCell
//cell.textLabel?.text = Data.product[indexPath.section].option[indexPath.row]
cell.textLabel?.text = data[indexPath.row]
return cell
}

但它在我的表格视图中向我显示了单个部分数据..

你在cellForRow方法中采用选项Data,该方法总是采用lastVisible data,你应该在你的didSelectRowAt方法中使用选项Data

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didselect")
optionData = Data.product[indexPath.row].option
performSegue(withIdentifier: "option", sender: self)
}

尝试分享您的结果。

最新更新