How do I pass data after reloading the cell to another view controller

380 Views Asked by At

I have a view controller which reorders the cell in first view controller but the reorder is not reflected in second view controller.I tried using structures to pass data but it was not possible.Using commonInit I pass details to the second view controller

import UIKit
import CoreData

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return name.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return  95
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! PizzaListTableViewCell
      /*  let data = pizza[indexPath.row]
        
        cell.textLabel?.text = data.name
        cell.textLabel?.text = data.miscellaneousText
        cell.textLabel?.text = data.amount */

        cell.commonInit(_imageName: "pizza_\(indexPath.item)", title:name[indexPath.item], text: miscellaneousText[indexPath.item], amount: amount[indexPath.item])
        return cell
    }
    func  tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = DetailsVC()
        vc.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
        
        self.navigationController?.pushViewController(vc, animated: true)
        self.tableView.deselectRow(at: indexPath, animated: true)
    }
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        
        let item = name[sourceIndexPath.row]
        name.remove(at: sourceIndexPath.row)
        name.insert(item, at: destinationIndexPath.row)
        
    }
     

Next View Controller name is DetailsVC().Reorder is not reflected in this view controller.

import UIKit
import CoreData

class DetailsVC: UIViewController, UIPickerViewDataSource ,UIPickerViewDelegate{
    let name = ["Bacon Cheese Fry","Pizza Margherita"]
    let miscellaneousText = ["Accepted","Accepted"]
    let amountArr = ["39.99","39.99"]
     
    @IBOutlet weak var detailImage: UIImageView!
    var imageName: String!
    var titleName: String!
    
    @IBOutlet weak var text: UILabel!
    var textName: String!
    
    var textDescript: String!
    
    @IBOutlet weak var amount: UILabel!
    var amountName: String!
     func commonInit(_ imageName: String, title: String, text: String, amount: String,pizzaTextField:String)
    {
        self.imageName = imageName
        self.title = title
        self.textName = text
        self.amountName = amount
        self.pizzaText = pizzaTextField
       
        
    }
1

There are 1 best solutions below

4
On
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
            let vc =  storyBoard.instantiateViewController(withIdentifier: "yourVCIdentifier") as? DetailsVC
            vc?.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
            self.navigationController?.pushViewController(vc, animated: true)
            self.tableView.deselectRow(at: indexPath, animated: true)
 }

Edit:

With XIB

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                let vc =  DetailVC(nibName: "YourNibName", bundle: nil)
                vc.commonInit("pizza_\(indexPath.item)", title:name[indexPath.item],text: miscellaneousText[indexPath.item], amount: amount[indexPath.item],pizzaTextField: pizzaToppings[indexPath.item])
                self.navigationController?.pushViewController(vc, animated: true)
                self.tableView.deselectRow(at: indexPath, animated: true)
}