【从零开始的Swift开发心路历程-Day20】简易订单系统Part4(完)

目前我们已经完成简易订单系统的新增订单及删除订单,只要加上修改订单的功能就算完成啦!
我们一样利用将TableView往左滑的方式来修改订单

而按下修改订单的按钮後会跳出UIAlertController来让我们修改订单名称及金额

废话不多说,马上开始实做吧!
首先我们一样需要这个function

但因为跟昨天所做的删除都是将TableView往左滑来达成,只是需要新增一个修改订单的按钮,所以只要在同样的function下新增程序码就可以了~

//左滑修改
        let modifyAction = UIContextualAction(style: .normal, title: "修改订单") { (action, sourceView, complete) in
            //呼叫提示框
            func createAlert(alertTitle: String, alertMessage: String, actionTitle: String, cancleTitle: String, indexPath: IndexPath?)
            {
                let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
                let cancleAction = UIAlertAction(title: cancleTitle, style: .cancel, handler: nil)
                let okAction = UIAlertAction(title: actionTitle, style: .default, handler: {
                    (action: UIAlertAction!) -> Void in
                    let modify: Order = Order()
                    let text0 = alert.textFields?[0].text
                    let text1 = alert.textFields?[1].text
                    modify.id = self.orders[indexPath!.row].setId
                    modify.name = text0!
                    modify.price = Int(text1!)!
                    try! self.realm.write {self.realm.add(modify, update: .all)}
                    self.allorders()
                })
                alert.addTextField { (titleTF) in
                    titleTF.placeholder = "订单名称"
                }
                alert.addTextField { (subtitleTF) in
                    subtitleTF.placeholder = "订单价钱"
                }
                alert.addAction(okAction)
                alert.addAction(cancleAction)
                self.present(alert, animated: true, completion: nil)
            }
            createAlert(alertTitle: "修改", alertMessage: "", actionTitle: "确认", cancleTitle: "取消", indexPath: indexPath)
            complete(true)
        }
        modifyAction.image = UIImage(systemName: "square.and.pencil")
        modifyAction.backgroundColor = UIColor.systemYellow

Realm的修改订单与新增订单雷同,当时在这里卡蛮久的
首先一样要查询订单的id,再将订单的名称及价钱置换成UIAlertController里面所输入的内容就可以了!
最後记得要在UISwipeActionsConfiguration里加入modifyAction

执行结果如下

如此一来,简易的订单系统就大功告成啦!!


<<:  [Angular] Day32. Lazy-loading feature modules

>>:  [C 语言笔记--Day20] 解题纪录 10190 Divide, But Not Quite Conquer!

[Day 30] 完赛心得 — 大家可以回家啦

完赛心得 转眼间就过了 30 天啦,第一次参赛有够菜没想到还能迎来这一天。 要坚持每天发文真的很考验...

【DAY 1】Microsoft 365 ,365天天都用的到的生产力工具

什麽是Microsoft 365? (+Microsoft 365 开发人员计画 (Microsof...

Vue3 ( CLI + github ) -4

1.SAP 多一个#前端所管理的路由,只会渲染部分区域 2.环境安装 CLI (1) 安装 Node...

Day 20 - Self-defined Data types(in C) 自订资料型态

Intro 自订资料型态可以是 把不同 data type 合成成一个 复合的型态 或是重新定义一个...

[D27] : 一个Queue+Docker在Local的实作(1/4)

没想到这周会炸忙 =皿= 这篇目的是想做到一个很简单的Docker和Message Queue实作...