2015-11-06 67 views
0

我想让用户修改/删除tableview中的默认文本,我已经尝试了以下代码巫婆的作品,但只要用户去其他视图控制器和回(或离开应用程序,并回来),文本可以追溯到默认文本。 我怎么能工作? 我已经发布了tableView的整个代码,所以你可以有我可以做错了什么的概述。 谢谢!如何编辑或删除tableView中的文本?

var places = [Dictionary<String,String>()] 

var activePlace = -1 

class TableViewController: UITableViewController { 

    func companyNameUpdatedAlert(title: String, error: String, indexPath: Int) { 

     let alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert) 

     alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 

      textField.placeholder = "Enter new text" 

     } 

     alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action) -> Void in 

      let lat = places[indexPath]["lat"]! 

      let lon = places[indexPath]["lon"]! 

      places.removeAtIndex(indexPath) 

      places.insert(["name" : alert.textFields![0].text!, "lat" : lat, "lon" : lon], atIndex: indexPath) 

      self.tableView.reloadData() 


     })) 

     self.presentViewController(alert, animated: true, completion: nil) 


    } 

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { 

     let changeText = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Change text" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in 

      self.companyNameUpdatedAlert("Update text", error: "enter text below", indexPath: indexPath.row) 

     }) 

     let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:NSIndexPath) -> Void in 

      places.removeAtIndex(indexPath.row) 

      tableView.reloadData() 

     }) 

     return [changeText, deleteAction] 


    } 

    override func viewDidLoad() { 

     //save start 

     if NSUserDefaults.standardUserDefaults().objectForKey("places") != nil { 

      places = NSUserDefaults.standardUserDefaults().objectForKey("places") as! [Dictionary] 


      //save stop 

     super.viewDidLoad() 



     if places.count == 1 { 

      places.removeAtIndex(0) 

      places.append(["name":"Long press on map to add location","lat":"90","lon":"90"]) 



     } 
     if NSUserDefaults.standardUserDefaults().objectForKey("places") != nil { 

      places = NSUserDefaults.standardUserDefaults().objectForKey("places") as! [Dictionary] 


     } 

    } 
    } 

    override func didReceiveMemoryWarning() { 

     super.didReceiveMemoryWarning() 

    } 

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

     return 1 

    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return places.count 


    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell 

     cell.textLabel?.text = places[indexPath.row]["name"] 

     return cell 



    } 

    override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 

     activePlace = indexPath.row 

     return indexPath 

    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

     if segue.identifier == "newPlace" { 

      activePlace = -1 


     } 

    } 

    override func viewWillAppear(animated: Bool) { 

     tableView.reloadData() 


    } 

} 

回答

0

当您编辑表格时,您应该将数据保存在NSUserDefaults中。否则,您只需更改每次加载类时从原始NSUserDefaults重新加载的类属性。

试试这个你UIAlertAction闭包:

NSUserDefaults.standardUserDefaults().setObject(places, forKey: "places") 
NSUserDefaults.standardUserDefaults().synchronize() 
+0

非常感谢,我一直停留在那一阵子 –

相关问题