#9.4點餐系統App
Published in
7 min readDec 3, 2022
程式重點:
- 上傳訂單
- 刪除表格
- 計算總價
感想:這是最後一個章節.實作這個App,當初也遇到很多困難,沒想到實際寫出來的文章內容,比預期還要少很多的感覺.當初寫的辛苦,半死不活的,現在回頭來看也完成了大部分的功能.
最近看了韓劇<二十五,二十一>,是關於韓國擊劍題材的青春戀愛劇.看的我少女心咪咪帽帽.其中有段話是女主角的爸爸講的台詞.很適合正在學程式,學任何自己對那項專業技能有興趣的大家.
「學習一項事物,遇到挫折時,正在盤旋時,大家總以為只要努力,實力就會像對角線一樣,平緩爬升.但事實上並不會.實力是像階梯一樣上升的,但大多數人在那階梯中,他們不知道在這之後,平坦的時期就放棄了,實力會突然陡峭的上升」
等我們爬的階梯夠多了,無意間,我們走的比之前更遠,看的更廣了.
大家加油!
- 上傳訂單
//接收的orderMenu
var orderListGet:orderMenu!
//新增表格的orderMenu
//全部訂單
var orderSum = [orderMenu]()
//上傳的資料
var orderList = [OrderListItem]()
override func viewDidLoad() {
super.viewDidLoad()
//下載
orderSum = orderMenu.loadOrderList() ?? []
//儲存
orderSum.append(orderListGet)
//計算總價
updateOrderSum()
}
func uploadOrder(){
let shopListUrl = URL(string: "https://api.airtable.com/v0/appk1fzVKpuilyG7v/orderList")!
var request = URLRequest(url: shopListUrl)
request.setValue("Bearer \(apikey)", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
//要記得加json的解析格式
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let encoder = JSONEncoder()
//呼叫上傳資料型別的陣列
var recrords = [Record4]()
//把訂單迴圈加入 上傳資料型別
for items in orderSum{
let list = Record4(fields: Fields4(itemName: items.itemName, priceSum: items.priceSume, toppingAndMeals: items.toppingAndMeals, textField: items.textField, itemNumber: items.itemNumber,toppingAll: items.toppingAll))
recrords.append(list)
}
//把資料裝上 上傳資料型別.進行解碼
let orderlist = OrderListItem(records: recrords)
request.httpBody = try? encoder.encode(orderlist)
print(String(data: request.httpBody!, encoding: .utf8)!)
URLSession.shared.dataTask(with: request) { data, response, error
in
if data != nil{
print(request)
DispatchQueue.main.async {
guard let navigationCotroller = self.navigationController else { return }
let count = navigationCotroller.viewControllers.count
let gobackTop = self.navigationController?.viewControllers[ count - 3] as! ShopSellTableViewController
navigationCotroller.popToViewController(gobackTop, animated: true)
//報錯可以使用 string(data,encoding)
let content = String(data: data!, encoding: .utf8)
}
}
}.resume()
orderSum.removeAll()
orderMenu.saveOrderList(orderList:orderSum)
tableView.reloadData()
}
- 計算總價
func updateOrderSum(){
//每次更新的時候都做一次計算
orderSumPrice = 0
//迴圈取出每筆訂單的價錢
for sum in orderSum {
orderSumPrice += sum.priceSume
}
buttonGotoshop.setTitle("繼續點餐\n $ \(orderSumPrice)", for: .normal)
}
//繼續點餐且存取資料
@IBAction func goBackShop(_ sender: UIButton) {
//判斷有無navigationController
if let navigationController = navigationController{
//計算viewControllers數量
let count = navigationController.viewControllers.count
//第二頁是-3 要記得轉型到想去的頁面
let gobackTop = navigationController.viewControllers[ count - 3] as! ShopSellTableViewController
//present 去 pop回
//show 去 diss回
navigationController.popToViewController(gobackTop, animated: true)
orderMenu.saveOrderList(orderList:orderSum)
print(orderMenu.documentsDirectory)
}
}
- 刪除表格
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
orderSum.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
orderMenu.saveOrderList(orderList:orderSum)
updateOrderSum()
tableView.reloadData()
}
程式碼: