title: Swift: How to use NSUserDefaults to store separate information for each cell in Table View
body: <p>and thanks in advance for helping me out if you can. I'm still very new to Swift but looking forward to learning quickly so I can help others here on the forum. </p>
  
  <p>For now, I have a problem that I need to solve and I have no idea where to start. Hypothetically, my goal is to achieve something like the following:</p>
  
  <p><strong>First View: User enters names of cities they have visited</strong></p>
  
  <p><strong>Second View: User enters names of museums they saw in that city</strong></p>
  
  <p><strong>Then, each time the user selects a city on the first View, the stored 'museum' information will be displayed.</strong> </p>
  
  <p>So far, I've managed to get NSUserDefaults functional, but the same data is shown regardless of the cell that I select from the list on the initial View. </p>
  
  <p>I am using the specific name of the array throughout the app to store, display and read data from NSUserDefaults, and I want to know the best way to edit this so that the app will check which cell I select and then display the appropriate information (or none if there hasn't been any entered for that cell). </p>
  
  <p>I hope that was a good explanation, I know it was long - sorry. Below is my code so far. Any input or suggestions are welcome. Thank you!</p>
  
  <pre><code>import UIKit
  import Foundation
  
  class DetailViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
  
      @IBOutlet weak var titleLabel: UILabel!
      @IBOutlet weak var detailTextField: UITextField!
      @IBOutlet weak var detailTableView: UITableView!
  
      var receivedString:String = ""        
      var cell0Array:[NSString] = []
  
      override func viewDidLoad() {
          super.viewDidLoad()
          detailTextField.delegate = self
          detailTableView.delegate = self
          detailTableView.dataSource = self
  
          titleLabel.text = receivedString
  
          if NSUserDefaults.standardUserDefaults().objectForKey("cell0Array") != nil {
          cell0Array = NSUserDefaults.standardUserDefaults().objectForKey("cell0Array") as! [NSString]
          } else {
          }
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
      }
  
      func textFieldShouldReturn(textField: UITextField) -&gt; Bool {
  
          let now = NSDate()
  
          let formatter = NSDateFormatter()
  
          formatter.dateStyle = .MediumStyle
          formatter.timeStyle = .NoStyle
  
          let date = formatter.stringFromDate(now)
  
          let detailTextFieldContents = detailTextField.text
          let token = "\(date): \(detailTextFieldContents!)lb"
          cell0Array.insert(token, atIndex: 0)
  
          NSUserDefaults.standardUserDefaults().setObject(cell0Array, forKey: "cell0Array")
          NSUserDefaults.standardUserDefaults().synchronize()
  
          detailTextField.text = ""
          detailTextField.resignFirstResponder()
  
          self.detailTableView.reloadData()
  
          return true
      }
  
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return 1
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return cell0Array.count
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          let detailCell = tableView.dequeueReusableCellWithIdentifier("detailCell", forIndexPath: indexPath)
  
          let detailItem = cell0Array[indexPath.row]
  
          detailCell.textLabel!.text = detailItem as String
          detailCell.textLabel!.textAlignment = .Center
  
          self.view.layoutIfNeeded()
          return detailCell
      }
  
  
  }
  </code></pre>