如何编辑或删除tableView中的文本?

时间:2015-11-06 16:51:05

标签: xcode swift

我正在尝试让用户修改/删除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()


    }

}

1 个答案:

答案 0 :(得分:0)

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

UIAlertAction关闭中尝试此操作:

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