gpt4 book ai didi

swift - UITableView内容删除后没有被删除? [Swift 3.0 - Xcode 8]

转载 作者:行者123 更新时间:2023-11-30 12:19:48 24 4
gpt4 key购买 nike

我试图删除 UITableView 中的单元格,但是当我删除它们时,它们会在我重新加载应用程序后重新出现。我从使用核心数据存储值的同一数组中删除。我是否也必须直接从 coreData 中删除?我怎么做?

这是我的代码:

///Function to remove TableView Cells

public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete
{
SetTIME.remove(at: indexPath.row)
if SaveData.count == 4 {
SaveData.remove(at: myIndex*4)
SaveData.remove(at: (myIndex*4)+1)
SaveData.remove(at: (myIndex*4)+2)
SaveData.remove(at: (myIndex*4)+3)}
tableView.reloadData()
}

}



///CoreData

override func viewDidLoad() {
super.viewDidLoad()

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let newTrip = NSEntityDescription.insertNewObject(forEntityName: "Sets", into: context)
if DataGrab.count == 4{ // need to change after storing data in VC2 before sending
newTrip.setValue(DataGrab[0], forKey: "set1")
newTrip.setValue(DataGrab[1], forKey: "set4")
newTrip.setValue(DataGrab[2], forKey: "set3")
newTrip.setValue(DataGrab[3], forKey: "set2")}


do {
try context.save()
print("Saved")
}
catch{
//ProcessError
}


let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Sets")
request.returnsObjectsAsFaults = false

do {
let results = try context.fetch(request)
if results.count > 0 {
for result in results {
if let set1 = (result as AnyObject).value(forKey: "set1") as? String
{
if SaveData.contains(set1){

} else {
SaveData.append(set1)
SetTIME.append(set1)

if let set2 = (result as AnyObject).value(forKey: "set2") as? String
{
SaveData.append(set2)
}
if let set3 = (result as AnyObject).value(forKey: "set3") as? String
{
SaveData.append(set3)
}
if let set4 = (result as AnyObject).value(forKey: "set4") as? String
{
SaveData.append(set4)
}
}
}
}
}
}
catch{
//ProcessErrors
}

最佳答案

您可以将 NSFetchedResultsController 与 UITableview 一起使用。这将负责核心数据中对象的删除/添加/编辑。

以下是创建 NSFetchedResultsController 的链接

https://cocoacasts.com/populate-a-table-view-with-nsfetchedresultscontroller-and-swift-3/

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/nsfetchedresultscontroller.html

关于swift - UITableView内容删除后没有被删除? [Swift 3.0 - Xcode 8],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44937644/

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