1

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 ..

1
  • 3
    First of all never name a custom class Data. It could interfere with the internal struct Data. And variable names are supposed to start always with a lowercase letter. And in table view delegate methods use always the passed tableView instance rather than the hard-coded properties. Commented Aug 23, 2018 at 8:37

1 Answer 1

1

You are taking the optionData in your cellForRow method, which is always taking the lastVisible data, You should take the optionData in your didSelectRowAt method

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

Try and share your results.

Sign up to request clarification or add additional context in comments.

7 Comments

Its sending First Section Data to next viewController (tableView) / A B,C
You should comment the gesture part from your cellForRow. These two line. let tapGeshture = UITapGestureRecognizer(target: self, action: #selector(tapped(_:))) cell.optionLabel.addGestureRecognizer(tapGeshture).
Did it but Same Result i think this two line not part of sending data
yes, that are not part of sending data, but I thought may be something wrong in tapped(_ :) method.
what is there in tapped(_ :) method, can you please post that code to better understand the issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.