如何保留主题 ID,在 TableViewCell 中标记为已完成
How to persist Topics ID, Which marked as completed in TableViewCell
我想在 TableView 中跟踪已完成的主题列表
I have set delegate which confirms to tableView topic mark as completed
protocol TopicDetialVCDelegate: class {
func hasBeenCompletedTopic()
}
TableViewVC
func hasBeenCompletedTopic() {
isPerformedDelegate = true
if !completedTopicIdArray.contains(completedTopicId) {
completedTopicIdArray.append(completedTopicId)
}
print("completed Topics \(completedTopicIdArray)")
print("TopicVC: Completed Topics total: \(completedTopicIdArray.count)")
}
This is working, but i want to persist always mark as completed which already marked
Here is code of CellForRowAt
if isPerformedDelegate {
for _ in 0...completedTopicIdArray.count {
if completedTopicIdArray.contains(filteredTopicArray[indexPath.row].id!) {
cell.topicCompletedTickImageView.image = #imageLiteral(resourceName: "Tick")
}
}
}
我想要什么
应该有一个数组来获取所有已完成的主题 idz,并且在应用程序的每个 运行 上检查单元格索引路径是否包含 topicID show Tick image
I can use UserDefaults like this
UserDefaults.standard.set(array, forkey: "abc")
但问题是数组将在应用程序的 运行 上再次重新初始化
like so in ViewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
UserDefaults.standard.set(completedTopicIdArray, forKey: "completedTopics")
UserDefaults.standard.synchronize()
}
accessing in ViewDidLoad
let topics = UserDefaults.standard.array(forKey: "completedTopics")
print(topics as? [Int] ?? [Int]())
我已经用 CoreData 解决了这个问题,想分享一下
Declare empty array
var completedTopicsIdPersistArray: [Int] = [Int]()
CoreData Method for Creating
func persistIdIntoCoreData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
guard let entity = NSEntityDescription.entity(forEntityName: "ComTopicDB", in: managedObjectContext)
else {
print("Entity Not Found")
return
}
let topicIdz = NSManagedObject(entity: entity, insertInto: managedObjectContext)
topicIdz.setValue(completedTopicId, forKey: "topicId")
do {
try managedObjectContext.save()
}
catch let error as NSError {
print("Unable to save into CoreData: \(error.userInfo)")
}
}
CoreData Method for Retrieving
func retrieveIdFromCoreData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "ComTopicDB")
do {
let topicIdz = try managedObjectContext.fetch(fetchRequest)
for id in topicIdz {
let unwrapId = id.value(forKey: "topicId") as! Int
if !completedTopicsIdPersistArray.contains(unwrapId) {
completedTopicsIdPersistArray.append(unwrapId)
}
}
}
catch let error as NSError {
print("Found issue during retrieve id from CoreData:\(error.userInfo)")
}
}
Calling inside of delegate method, which confirms topic completed
func hasBeenCompletedTopic() {
isPerformedDelegate = true
if !completedTopicIdArray.contains(completedTopicId) {
completedTopicIdArray.append(completedTopicId)
persistIdIntoCoreData()
}
print("completed Topics \(completedTopicIdArray)")
print("TopicVC: Completed Topics total: \(completedTopicIdArray.count)")
retrieveIdFromCoreData()
}
and finally CellForRowAt Method
if completedTopicsIdPersistArray.contains(filteredTopicArray[indexPath.row].id!) {
cell.topicCompletedTickImageView.image = #imageLiteral(resourceName: "Tick")
}
Finally call inside ViewWillAppear, because it comes from topicDetailVC back
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
retrieveIdFromCoreData()
}
现在所有主题都会显示勾号图标,标记为已完成,甚至终止并重新启动应用程序
我想在 TableView 中跟踪已完成的主题列表
I have set delegate which confirms to tableView topic mark as completed
protocol TopicDetialVCDelegate: class {
func hasBeenCompletedTopic()
}
TableViewVC
func hasBeenCompletedTopic() {
isPerformedDelegate = true
if !completedTopicIdArray.contains(completedTopicId) {
completedTopicIdArray.append(completedTopicId)
}
print("completed Topics \(completedTopicIdArray)")
print("TopicVC: Completed Topics total: \(completedTopicIdArray.count)")
}
This is working, but i want to persist always mark as completed which already marked
Here is code of CellForRowAt
if isPerformedDelegate {
for _ in 0...completedTopicIdArray.count {
if completedTopicIdArray.contains(filteredTopicArray[indexPath.row].id!) {
cell.topicCompletedTickImageView.image = #imageLiteral(resourceName: "Tick")
}
}
}
我想要什么
应该有一个数组来获取所有已完成的主题 idz,并且在应用程序的每个 运行 上检查单元格索引路径是否包含 topicID show Tick image
I can use UserDefaults like this
UserDefaults.standard.set(array, forkey: "abc")
但问题是数组将在应用程序的 运行 上再次重新初始化
like so in ViewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
UserDefaults.standard.set(completedTopicIdArray, forKey: "completedTopics")
UserDefaults.standard.synchronize()
}
accessing in ViewDidLoad
let topics = UserDefaults.standard.array(forKey: "completedTopics")
print(topics as? [Int] ?? [Int]())
我已经用 CoreData 解决了这个问题,想分享一下
Declare empty array
var completedTopicsIdPersistArray: [Int] = [Int]()
CoreData Method for Creating
func persistIdIntoCoreData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
guard let entity = NSEntityDescription.entity(forEntityName: "ComTopicDB", in: managedObjectContext)
else {
print("Entity Not Found")
return
}
let topicIdz = NSManagedObject(entity: entity, insertInto: managedObjectContext)
topicIdz.setValue(completedTopicId, forKey: "topicId")
do {
try managedObjectContext.save()
}
catch let error as NSError {
print("Unable to save into CoreData: \(error.userInfo)")
}
}
CoreData Method for Retrieving
func retrieveIdFromCoreData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedObjectContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "ComTopicDB")
do {
let topicIdz = try managedObjectContext.fetch(fetchRequest)
for id in topicIdz {
let unwrapId = id.value(forKey: "topicId") as! Int
if !completedTopicsIdPersistArray.contains(unwrapId) {
completedTopicsIdPersistArray.append(unwrapId)
}
}
}
catch let error as NSError {
print("Found issue during retrieve id from CoreData:\(error.userInfo)")
}
}
Calling inside of delegate method, which confirms topic completed
func hasBeenCompletedTopic() {
isPerformedDelegate = true
if !completedTopicIdArray.contains(completedTopicId) {
completedTopicIdArray.append(completedTopicId)
persistIdIntoCoreData()
}
print("completed Topics \(completedTopicIdArray)")
print("TopicVC: Completed Topics total: \(completedTopicIdArray.count)")
retrieveIdFromCoreData()
}
and finally CellForRowAt Method
if completedTopicsIdPersistArray.contains(filteredTopicArray[indexPath.row].id!) {
cell.topicCompletedTickImageView.image = #imageLiteral(resourceName: "Tick")
}
Finally call inside ViewWillAppear, because it comes from topicDetailVC back
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
retrieveIdFromCoreData()
}
现在所有主题都会显示勾号图标,标记为已完成,甚至终止并重新启动应用程序