重新排序删除uitaiteViewCell行时设置的字典
我决定使用字典通过 API 调用填充多个 tableView,而不是使用数组和异步等待来减少加载时间。我在后台线程中异步调用所有 API,为每个字典提供从 0 开始递增的索引,当所有 API 调用完成后,我对字典进行排序
let sortedDictionary = dictionary.sorted {
return $0.key < $1.key
}
:比按顺序运行 API 调用快得多。现在我必须以某种方式在删除一行时对 tableView 进行重新排序,就像对标准数组一样(我有一个包含多个 tableView 的滚动视图,以及另一个每个 tableView 都有一行的 tableView ;第二个 tableView 是我要删除行的那个 tableView )。这是我的尝试,但是我的逻辑中的某些内容关闭了,或者我遗漏了某些内容:
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
//delete object from dictionary
self.dictionary.removeValue(forKey: indexPath.row)
for i in indexPath.row...dictionary.count-1{
if i == dictionary.count-1{
//remove last index in the dictionary at end of loop
self.dictionary.removeValue(forKey: i)
} else{
//increment all dictionary indices past the deleted row down one
let j = i+i
let value = dictionary[j]
guard let value = value else {return}
self.dictionary.removeValue(forKey: j)
self.dictionary.updateValue(value, forKey: i)
}
}
}
}
似乎我在这里遇到了空值:
guard let value = value else {return}
这不是存储字典值的正确方法吗?
let value = dictionary[j]
任何帮助表示赞赏。谢谢!
I decided to use dictionaries to populate multiple tableViews with API calls as opposed to using arrays and async await to reduce load times. I call all of my API's in a background thread asynchronously, giving each dictionary an incremented index from 0 up, and when all the API calls are done I sort the dictionary as such:
let sortedDictionary = dictionary.sorted {
return $0.key < $1.key
}
Then I populate my tableViews; much faster than running the API calls in sequence. Now I have to somehow reorder the tableViews upon deleting a row like you would to a standard array (I have a scrollView with several tableViews, and another tableView that has a row for each tableView; this second tableView is the one which I am deleting rows). This is my attempt, but something in my logic is off, or I'm missing something:
func tableView(_ tableView: UITableView, commit editingStyle:
UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
//delete object from dictionary
self.dictionary.removeValue(forKey: indexPath.row)
for i in indexPath.row...dictionary.count-1{
if i == dictionary.count-1{
//remove last index in the dictionary at end of loop
self.dictionary.removeValue(forKey: i)
} else{
//increment all dictionary indices past the deleted row down one
let j = i+i
let value = dictionary[j]
guard let value = value else {return}
self.dictionary.removeValue(forKey: j)
self.dictionary.updateValue(value, forKey: i)
}
}
}
}
It seems that I'm running into a null here:
guard let value = value else {return}
Is this not the correct way to store a dictionary value?
let value = dictionary[j]
Any help is appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了问题,并将其格式化为模板函数,以便可以在多个字典模型上使用。如上所述,词典可以简单地进行分类,但这是另一种方法:
I resolved the issue and formatted it into a templated function so that it can be used on multiple dictionary models. As comments above mentioned, dictionaries can simply be sorted, but this is another way to do it: