Joe
Joe

Reputation: 8986

Saving TableView Array at indexPath in Swift

I have two tableView running in my project.I have successfully passed data from my first tableView to second tableView controller using segue.I am trying to save the data which i passed to secondViewController using NSUserDefaults.My partial code below....

First VC:

 var tableView: UITableView!
 var DataArray = ["Bus","Helicopter","Truck","Boat","Bicycle","Motorcycle","Plane","Train","Car","S    cooter","Caravan"]
 var sendSelectedData = NSString()


 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 let indexPath = tableView.indexPathForSelectedRow!
 let currentCell = tableView.cellForRow(at: indexPath) as UITableViewCell!
       // print(indexPath)
       // print(currentCell)

 sendSelectedData = (currentCell!.textLabel?.text)! as String as NSString
 performSegue(withIdentifier: "ShowDetails", sender: indexPath)

}

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    let selectedIndex = sender as! NSIndexPath
    let currentCell =  tableView.cellForRow(at: selectedIndex as IndexPath)!
    self.sendSelectedData = (currentCell.textLabel?.text)! as String as NSString
    if segue.identifier == "ShowDetails" {

    let viewController = segue.destination as! SecondController
    viewController.newItem = (self.sendSelectedData as String)  

    }
}

Second VC:

var NewArray = [""]

var newItem: String = ""

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    NewArray.insert(newItem, at: Array.count)
    self.tableView?.beginUpdates()

   // NewArray.append(newItem)

    let defaults = UserDefaults.standard
    defaults.object(forKey: "NewArray")

    self.tableView?.endUpdates()
  }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell

    cell.textLabel?.text = NewArray[(indexPath as NSIndexPath).row] as String
    return cell
  }

   func insertRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {
  }

   func reloadRowsAtIndexPaths(indexPaths: [AnyObject], withRowAnimation animation: UITableViewRowAnimation) {

  }

I am following https://grokswift.com/uitableview-updates/ article to achieve the function....

Thanks in Advance.

Upvotes: 0

Views: 817

Answers (1)

vadian
vadian

Reputation: 285064

In AppDelegate applicationDidFinishLaunching register an empty array as default value for the key "NewArray".

let defaultValues = ["NewArray": [String]()]
UserDefaults.standard.register(defaults: defaultValues)

In Second VC define newArray (variable names are supposed to start with a lowercase letter) also as empty String array

var newArray = [String]()

In viewDidLoad retrieve the array from user defaults, append the new item, save the array back to user defaults and reload the table view

override func viewDidLoad() {
  super.viewDidLoad()

  let defaults = UserDefaults.standard
  newArray = defaults.array(forKey: "NewArray") as! [String]
  newArray.append(newItem)
  defaults.set(newArray, forKey: "NewArray")
  self.tableView?.reloadData()
}

beginUpdates(), endUpdates() and insertRowsAtIndexPath / reloadRowsAtIndexPaths are not needed at all.

Upvotes: 1

Related Questions