i have a model object it have a array i m populating data by indexPath.row than i need to send every section array to another tableView but it sending me random data how can i send specific selected rows array to next tableView ? please help me to make understand ..
here is My Model Object :
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
}
}
Here is my Model Object :
class Data {
static var product = [ProductModel]()
}
Here is Data function For Append Data
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()
}
}
}
Here is first ViewDidLoad and a global array for store cell arary :
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()
}
}
}
Here is cellForRowAtIndex here i created in my label i add a tap Gesture .when user click it show another tableView with this cell array data
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
}
Here is prepare Segue and sending Data to next view controller :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "option") {
let dest = segue.destination as! OptionViewController
dest.data = optionData
}
}
Here is my nextViewController (For show option Data):
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()
}
}
}
Here option View Controller and in this controller i want to show retrieved array show in tableView
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
}
But its showing me single section data in my tableView ..
Data. It could interfere with the internal structData. And variable names are supposed to start always with a lowercase letter. And in table view delegate methods use always the passedtableViewinstance rather than the hard-coded properties.