iT邦幫忙

2025 iThome 鐵人賽

DAY 12
0
Mobile Development

Xcode x Swift Vibe coding進階開發之旅系列 第 12

Xcode x Swift Vibe coding進階開發之旅 第十二天 點名系統(5) 增加人員程式

  • 分享至 

  • xImage
  •  

說明

先實作realm新增方面的操作 /images/emoticon/emoticon08.gif

指令

https://ithelp.ithome.com.tw/upload/images/20250926/201681873nPzqdRdU6.png

結果

https://ithelp.ithome.com.tw/upload/images/20250926/20168187ye1b18lFxi.png
https://ithelp.ithome.com.tw/upload/images/20250926/201681874QCXlyoUT5.png

心得

很常在AI收到的是相較少時,AI容易沒事找事像是這次指令中沒有提到tableview,AI看到有宣告且項目少還有架構尚未完成時會主動實作,所以在coding時盡量提適中的指令長度,可以避免AI做出超出指令的行動同時又不會做太多而導致多錯

程式碼

 // 存儲所有使用者的陣列
    private var users: Results<user>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
   
        // 設置 TableView
        setupTableView()
        
        // 載入使用者數據
        loadUsers()
    }
    // 添加人員到數據庫
    private func addPersonToDatabase(userId: String, name: String) {
        let realm = try! Realm()
        
        // 檢查使用者 ID 是否已存在
        if let _ = realm.objects(user.self).filter("userId == %@", userId).first {
            showErrorAlert(message: "使用者 ID 已存在")
            return
        }
        
        // 建立新使用者
        let newUser = user(userId: userId, name: name)
        
        do {
            try realm.write {
                realm.add(newUser)
            }
            showSuccessAlert(message: "使用者新增成功")
            
            // 重新加載表格
            tbvselect.reloadData()
        } catch {
            showErrorAlert(message: "儲存失敗: \(error.localizedDescription)")
        }
    }
    // 載入使用者數據
    private func loadUsers() {
        let realm = try! Realm()
        users = realm.objects(user.self).sorted(byKeyPath: "createdAt", ascending: false)
        tbvselect.reloadData()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadUsers() // 每次視圖出現時重新加載數據
    }
    extension MainViewController: UITableViewDataSource, UITableViewDelegate {
    
    // 返回表格有幾個區域
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    // 返回每個區域有多少行
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users?.count ?? 0
    }
    
    // 返回每一行的內容
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell", for: indexPath)
        
        // 獲取對應的使用者資料
        if let currentUser = users?[indexPath.row] {
            cell.textLabel?.text = "\(currentUser.Name) (\(currentUser.userId))"
        } else {
            cell.textLabel?.text = "未知使用者"
        }
        
        return cell
    }
    
    // 行被選中時的處理
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        // 這裡可以添加點擊單元格後的處理代碼
        // 例如導航到使用者詳細資料頁面
    }
}

/images/emoticon/emoticon07.gif


上一篇
Xcode x Swift Vibe coding進階開發之旅 第十一天 非同步(3) async/await與執行緒
系列文
Xcode x Swift Vibe coding進階開發之旅12
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言