gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:56:22 25 4
gpt4 key购买 nike

我正在尝试让用户修改/删除表格 View 中的默认文本,我尝试了以下代码,但只要用户转到另一个 View Controller 并返回(或离开应用程序并返回) ,文本将恢复为默认文本。我怎样才能让它发挥作用?我已经发布了 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()


}

}

最佳答案

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

UIAlertAction 闭包中尝试一下:

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

关于xcode - 如何编辑或删除 tableView 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33571713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com