Ios 如何在Swift 3中编辑表格单元格的内容

Ios 如何在Swift 3中编辑表格单元格的内容,ios,swift,swift3,Ios,Swift,Swift3,我是Swift 3的初学者。我有一个表视图,用户可以删除一个表视图单元格。现在我希望用户能够更改单元格的内容。我有一个数组,其中包含四个名称[“Stremmel”、“Emma”、“Sam”、“Daisy”],我希望用户能够对George说edit Stremmel。 我搜索了一些文档或类似的问题,这些可以帮助我找到一种方法,但我更加困惑了。有人能帮我一下吗!!非常感谢。以下是我的表格视图: import UIKit var list = ["Stremmel" , "Emma" , "Sam"

我是Swift 3的初学者。我有一个表视图,用户可以删除一个表视图单元格。现在我希望用户能够更改单元格的内容。我有一个数组,其中包含四个名称[“Stremmel”、“Emma”、“Sam”、“Daisy”],我希望用户能够对George说edit Stremmel。
我搜索了一些文档或类似的问题,这些可以帮助我找到一种方法,但我更加困惑了。有人能帮我一下吗!!非常感谢。以下是我的表格视图:

import UIKit
var list = ["Stremmel" , "Emma" , "Sam" , "Daisy"]
class ViewController: UITableViewController {

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
   return list.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.textLabel?.text = list[indexPath.row]
    return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle:  UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == UITableViewCellEditingStyle.delete
{
     list.remove(at: indexPath.row)
     tableView.reloadData()
}


}

通常,在创建
UITableViewController()
类时,您应该拥有一些模板代码,这些代码提供编辑按钮和删除功能(应包含在编辑按钮中)!只需取消注释,就可以访问它了

或者您可以在viewDidLoad()函数中调用self.editButtonItem()


很抱歉我的英语不好,我希望这能回答你的问题

如果您想用Delete按钮同时显示Edit按钮,则需要用
canEditRowAt
方法实现
editActionsForRowAt
方法,而不是
commit editingStyle

之后,使用
editActionsForRowAt
使用
textField
显示
AlertController
,更新其值并重新加载行。因此,从代码中删除或注释
commit editingStyle
方法,并添加以下两种方法

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, indexPath) in
        let alert = UIAlertController(title: "", message: "Edit list item", preferredStyle: .alert)
        alert.addTextField(configurationHandler: { (textField) in
            textField.text = self.list[indexPath.row]
        })
        alert.addAction(UIAlertAction(title: "Update", style: .default, handler: { (updateAction) in
            self.list[indexPath.row] = alert.textFields!.first!.text!
            self.tableView.reloadRows(at: [indexPath], with: .fade)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        self.present(alert, animated: false)
    })

    let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, indexPath) in
        self.list.remove(at: indexPath.row)
        tableView.reloadData()
    })

    return [deleteAction, editAction]
}

对不起,我不太明白你的回答,但我感谢你的帮助。我能问一下第一个是什么意思吗?(self.list[indexPath.row]=alert.textFields!.first!.text!)@D.Moses AlertController的属性类型为
[UItextField]
,我们在alert中有一个textfield,因此我们使用
textFields
数组的第一个属性访问它。这非常有用。谢谢你的澄清。