gpt4 book ai didi

ios - 如何使用 Swift 从 tableview 和 sqlite3 中删除

转载 作者:行者123 更新时间:2023-11-28 07:29:18 25 4
gpt4 key购买 nike

我是 swift 和 sqlite3 的新手,我需要有关如何从 tableview 和 sql db 中删除的帮助。

我尝试使用 reloadData() 但它不起作用。我尝试使用 tableView.deleteRows(at: [indexPath], with: .fade) 进行删除,但我收到错误消息,因为我在此之前运行了一个 sql delete 语句。使用下面提供的这段代码,我成功地从数据库中删除了该项目,但它不会刷新 tableview。我暂时解决它的方法是在成功删除一个项目后执行到前一个屏幕的转换,当返回到 tableviewcontroller 时它将被删除。

import UIKit

class TableViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

let mainDelegate = UIApplication.shared.delegate as! AppDelegate
@IBOutlet var tableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? SiteCell ?? SiteCell(style: .default, reuseIdentifier: "cell")
let rowNum = indexPath.row
tableCell.primaryLabel.text = mainDelegate.people[rowNum].name
tableCell.secondaryLabel.text = mainDelegate.people[rowNum].email
tableCell.myImageView.image = UIImage(named: mainDelegate.people[rowNum].avatar!)
tableCell.accessoryType = .disclosureIndicator
return tableCell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mainDelegate.people.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

let rowNum = indexPath.row
let details : String! = "Address: \(mainDelegate.people[rowNum].address!) \nPhone Num: \(mainDelegate.people[rowNum].phonenum!) \nEmail: \(mainDelegate.people[rowNum].email!) \nAge: \(mainDelegate.people[rowNum].age!) \nGender: \(mainDelegate.people[rowNum].gender!) \nDate of birth: \(mainDelegate.people[rowNum].dob!)"
let alertController = UIAlertController(title: mainDelegate.people[rowNum].name, message: details, preferredStyle: .alert
)
let cancelAction = UIAlertAction(title: "ok", style: .cancel, handler: nil)
print("TESTING ROW: \(mainDelegate.people[rowNum].id!)")
alertController.addAction(cancelAction)
present(alertController, animated: true)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
var rowNum: Int = indexPath.row
if editingStyle == .delete {
print("Testing delete \(mainDelegate.people[rowNum].id!)")
print("\(indexPath.row)")

mainDelegate.removeFromDatabase(id: mainDelegate.people[rowNum].id!)
print("\(indexPath)")
// tableView.deleteRows(at: [indexPath], with: .fade)

DispatchQueue.main.async{
self.tableView.reloadData()
}
// self.performSegue(withIdentifier: "DataToInfo", sender: self)

// let mainDelegate = UIApplication.shared.delegate as! AppDelegate
// mainDelegate.removeFromDatabase(person: mainDelegate.people[indexPath.row])
}
}
override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
mainDelegate.readDataFromDatabase()
}

removeFromDatabase 方法

func removeFromDatabase(id : Int){

var db : OpaquePointer? = nil

if sqlite3_open(self.databasePath, &db) == SQLITE_OK{

print("Successfully opened connection to database at \(self.databasePath)")
var deleteStatement : OpaquePointer? = nil
let deleteStatementString : String = "delete from entries where id=\(id)"
if sqlite3_prepare_v2(db, deleteStatementString, -1, &deleteStatement, nil) == SQLITE_OK{
if sqlite3_step(deleteStatement) == SQLITE_DONE{
print("Deleted")
}
else{
print("Failed")
}
}else{
print("Couldn't prepare")
}
sqlite3_finalize(deleteStatement)

sqlite3_close(db)
}
}

我正在尝试从 TableView 和数据库中删除它。有一次我试图mainDelegate.people.remove(位于:indexPath.row) tableView.deleteRows(位于:[indexPath],带有:.fade)然后运行 ​​removeFromDatabase,但它给我一个错误。

最佳答案

您应该更新您的数据源。尝试像这样重构您的 commitEditing:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
var rowNum: Int = indexPath.row
if editingStyle == .delete {
print("Testing delete \(mainDelegate.people[rowNum].id!)")
print("\(indexPath.row)")

mainDelegate.removeFromDatabase(id: mainDelegate.people[rowNum].id!)
print("\(indexPath)")


mainDelegate.readDataFromDatabase()


tableView.deleteRows(at: [indexPath], with: .fade)


}
}

关于ios - 如何使用 Swift 从 tableview 和 sqlite3 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com