如何通过UITableView尾部滑动操作实现列表数据编辑更新
调整方案
你当前代码的核心问题有两个:
- 滑动方法用错了,
leadingSwipeActionsConfigurationForRowAt对应的是从屏幕左侧向右滑的操作,你要的从右向左滑(尾部滑动)应该用trailingSwipeActionsConfigurationForRowAt - 缺少编辑完成后回传更新数据、刷新列表的逻辑,按以下步骤修改即可:
第一步:修改ViewController2(列表页)代码
- 替换滑动方法为尾部滑动,同时传递当前编辑行的下标
- 新增监听编辑完成的通知,更新数据源刷新列表
- 加通知移除逻辑避免内存泄漏
class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource { var arrname = NSMutableArray() // 注意你原来的代码漏了tblView的IBOutlet,要记得加上 @IBOutlet weak var tblView: UITableView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // 监听编辑页传回的更新通知 NotificationCenter.default.addObserver( self, selector: #selector(updateListData(notification:)), name: NSNotification.Name(rawValue: "updateData"), object: nil ) } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell let dict = arrname[indexPath.row] as? NSMutableDictionary cell.txtFldName.text = dict!["name"] as? String cell.txtFldEmail.text = dict!["email"] as? String cell.txtFldPhone.text = dict!["phone"] as? String return cell } // 原有删除逻辑可以保留,也可以整合到滑动操作里 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { arrname.removeObject(at: indexPath.row) tblView.reloadData() } } // 替换为尾部滑动方法(从右向左滑触发) func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let editAction = UIContextualAction(style: .normal, title: "编辑") { [weak self] (action, view, completionHandler) in self?.handlerfun(index: indexPath.row) completionHandler(true) } editAction.backgroundColor = .systemPink // 如果需要保留删除操作,可以在这里加删除的UIContextualAction // let deleteAction = UIContextualAction(style: .destructive, title: "删除") { [weak self] _,_,_ in // self?.arrname.removeObject(at: indexPath.row) // self?.tblView.reloadRows(at: [indexPath], with: .automatic) // } return UISwipeActionsConfiguration(actions: [editAction]) } private func handlerfun(index:Int){ let dict = arrname[index] // 传数据到编辑页的同时带上当前行的下标 NotificationCenter.default.post( name: NSNotification.Name(rawValue: "data"), object: dict, userInfo: ["index": index] ) self.navigationController?.pushViewController(ViewController(), animated: true) // 注意:如果你当前的页面栈是编辑页在前,列表页在后,才用pop,正常逻辑应该是列表页跳转编辑页用push,上面的push替换你原来的pop } // 收到编辑完成的通知后更新列表 @objc func updateListData(notification: Notification) { guard let updatedDict = notification.object as? NSMutableDictionary, let index = notification.userInfo?["index"] as? Int, index < arrname.count else { return } arrname.replaceObject(at: index, with: updatedDict) // 只刷新对应行,性能更好 tblView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic) } // 移除通知观察者 deinit { NotificationCenter.default.removeObserver(self) } }
第二步:修改ViewController(编辑页)代码
- 存储当前编辑的行下标
- 新增保存按钮点击逻辑,把修改后的数据回传给列表页
- 加通知移除逻辑
class ViewController: UIViewController,UITextFieldDelegate { @IBOutlet weak var txtFldName:UITextField! @IBOutlet weak var txtFldEmail:UITextField! @IBOutlet weak var txtFldPhone:UITextField! var namearr = NSMutableArray() // 存储当前编辑的行下标 private var editIndex: Int? override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver( self, selector: #selector(fillEditData(notification:)), name: NSNotification.Name(rawValue: "data"), object: nil ) } @objc func fillEditData(notification:Notification) { if let data = notification.object as? NSMutableDictionary, let index = notification.userInfo?["index"] as? Int { txtFldName.text = data["name"] as? String txtFldPhone.text = data["phone"] as? String txtFldEmail.text = data["email"] as? String editIndex = index } } // 绑定到你编辑页的保存按钮点击事件 @IBAction func saveEdit(_ sender: UIButton) { // 输入合法性校验,可按需扩展 guard let name = txtFldName.text, !name.isEmpty, let email = txtFldEmail.text, !email.isEmpty, let phone = txtFldPhone.text, !phone.isEmpty, let index = editIndex else { return } // 封装修改后的数据 let updatedDict = NSMutableDictionary(dictionary: [ "name": name, "email": email, "phone": phone ]) // 回传数据到列表页 NotificationCenter.default.post( name: NSNotification.Name(rawValue: "updateData"), object: updatedDict, userInfo: ["index": index] ) // 返回列表页 navigationController?.popViewController(animated: true) } // 移除通知观察者 deinit { NotificationCenter.default.removeObserver(self) } }
可选优化建议
- 推荐把OC类型的
NSMutableArray、NSMutableDictionary换成Swift原生的[[String: String]]或者自定义结构体数组,避免强制类型转换的崩溃风险 - 通知耦合度较高,更推荐用闭包或者代理模式实现页面间数据传递,维护性更好
- 如果不需要系统默认的删除样式,可以删除
commit editingStyle方法,把删除操作整合到尾部滑动按钮里
内容的提问来源于stack exchange,提问作者AMIT KUMAR JHAMB
相关产品推荐
相关产品推荐

