gpt4 book ai didi

ios - 如何保留 TableView 行?

转载 作者:行者123 更新时间:2023-11-30 11:31:44 26 4
gpt4 key购买 nike

我正在创建一个ios应用程序,它将每天向我显示一组特定于用户的任务,我单击删除以显示其完成情况。我将任务保存在核心数据中,然后单击即可删除表格 View 行。我不会删除 coredata 中用户定义的数据,并且需要每天重新加载。如果应用程序在新的一天打开,我使用 newDay() 函数来决定从 coredata 加载数据。我应该怎么做才能记住当天已完成的所有任务?我是否需要创建另一个实体来记住所有任务已完成,或者是否有更简单的方法?

var tasks: [NSManagedObject] = []
let defaults = UserDefaults.standard
var calender = Calendar.current

override func viewDidLoad() {
super.viewDidLoad()

title = "DailyTasker"
navigationItem.leftBarButtonItem = editButtonItem

}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

let checkDate = newDay()
if checkDate{

//1
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}

let managedContext =
appDelegate.persistentContainer.viewContext

//2
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "Task")

//3
do {
tasks = try managedContext.fetch(fetchRequest)
defaults.set(Date(), forKey: "LastRun")
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
}

func newDay() -> Bool{
if let lastRun = defaults.object(forKey: "LastRun") as? Date{
if !calender.isDateInToday(lastRun){
return true
} else {
return false
}
} else {
return true
}

}


@IBAction func addName(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "New Task",
message: "Add a new task",
preferredStyle: .alert)

let saveAction = UIAlertAction(title: "Save",
style: .default) {
[unowned self] action in

guard let textField = alert.textFields?.first,
let nameToSave = textField.text else {
return
}

self.save(name: nameToSave)
self.tableView.reloadData()
}

let cancelAction = UIAlertAction(title: "Cancel",
style: .default)

alert.addTextField()

alert.addAction(saveAction)
alert.addAction(cancelAction)

present(alert, animated: true)
}

func save(name: String) {

guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}

// 1
let managedContext =
appDelegate.persistentContainer.viewContext

// 2
let entity =
NSEntityDescription.entity(forEntityName: "Task",
in: managedContext)!

let task = NSManagedObject(entity: entity,
insertInto: managedContext)

// 3
task.setValue(name, forKeyPath: "name")

// 4
do {
try managedContext.save()
tasks.append(task)
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return tasks.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let task = tasks[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskerCell", for: indexPath)
cell.textLabel?.text = task.value(forKeyPath: "name") as? String

return cell
}

// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {

}
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tasks.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}

}

最佳答案

您可以向任务添加日期属性,并将其命名为 lastDone。然后,您可以在任务完成时将其设置为当前日期时间,并在获取任务实例时使用谓词,这样您就只能获取今天未完成的任务。

 task.lastDone = Date()

我不确定你如何定义“今天”,但是this question应该帮助您创建一个谓词来正确过滤您的任务,尽管您可能还希望包含 lastDone 为 null 的任务。

关于ios - 如何保留 TableView 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50180733/

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