ios - Reloading and saving items in tableviews -


so creating tableview add (and delete) items. able add , delete items in tableview but...there issues...

  1. when add item not show in tableview until close simulator, when close , relaunch app appears in tableview.

  2. when add item tableview , close app, old items replaced new added item. auto-deletes old items when new item added tableview.

    naturally want save existing items when close app, , want add new items on top of existing tableview items without having close app , relaunch...i not sure wrong code...? thank in advance!

tableview

import uikit  class placetablevc : uitableviewcontroller {  var placelist = [string]()  override func viewdidload() {     super.viewdidload()      if nsuserdefaults.standarduserdefaults().objectforkey("list")  != nil {         placelist = nsuserdefaults.standarduserdefaults().objectforkey("list") as![string]      }      self.tableview.delegate = self   }  // mark: - table view data source  override func numberofsectionsintableview(tableview: uitableview) -> int {      return 2 }   // define number of different cells in tableview  override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int {     if(section == 0) {         return 1     }     else {         return placelist.count     } }   // define number of different cells in tableview  override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell {      switch indexpath.section{     case 0:         let myprofile = tableview.dequeuereusablecellwithidentifier("addaplacebutton") as! tableviewcell          return myprofile     default:         let cell = tableview.dequeuereusablecellwithidentifier("addedplacecell") as! secondtableviewcell          cell.textlabel?.text = placelist[indexpath.row]          return cell     }   }   override func tableview(tableview: uitableview, heightforrowatindexpath indexpath: nsindexpath) -> cgfloat {      let section = indexpath.section     if(section == 0) {         return 60     }     else {         return 60     } }  override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {     if editingstyle == uitableviewcelleditingstyle.delete {         placelist.removeatindex(indexpath.row)         nsuserdefaults.standarduserdefaults().setobject(placelist, forkey: "list")         tableview.reloaddata()     }     func viewdidappear(animated: bool) {         tableview.reloaddata()       } }  }   

add item code

import uikit  var placelist = [string]()  class addaplacemodalvc: uiviewcontroller, uitextfielddelegate {  @iboutlet var textfield: uitextfield!  @ibaction func additem(sender:anyobject) {     placelist.append(textfield.text!)     textfield.text = ""      nsuserdefaults.standarduserdefaults().setobject(placelist, forkey: "list")  }   override func viewdidload() {     super.viewdidload()     self.textfield.delegate = self }  override func didreceivememorywarning() {     super.didreceivememorywarning() }  override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {     self.view.endediting(true) }  func textfieldshouldreturn(textfield: uitextfield) -> bool {     textfield.resignfirstresponder()     return true  }  @ibaction func closeaddaplacevc(sender: anyobject) {     self.dismissviewcontrolleranimated(true, completion: nil)  }  } 

in class "placetable vc", try putting function:

override func viewwillappear(animated: bool) {     super.viewwillappear(animated)     self.tableview.reloaddata() } 

and function remove viewdidiappear:

override func tableview(tableview: uitableview, commiteditingstyle editingstyle: uitableviewcelleditingstyle, forrowatindexpath indexpath: nsindexpath) {     if editingstyle == uitableviewcelleditingstyle.delete {         placelist.removeatindex(indexpath.row)         nsuserdefaults.standarduserdefaults().setobject(placelist, forkey: "list")         tableview.reloaddata()     } } 

Comments