重新排序删除uitaiteViewCell行时设置的字典

发布于 2025-01-17 15:51:07 字数 1490 浏览 3 评论 0原文

我决定使用字典通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2025-01-24 15:51:07

我解决了问题,并将其格式化为模板函数,以便可以在多个字典模型上使用。如上所述,词典可以简单地进行分类,但这是另一种方法:

func deleteRows<T>(indexPath: IndexPath, dictionary: inout Dictionary<Int, T>){
    
    if indexPath.row == dictionary.count-1{
        //if its the last object in the tableView, delete and return
        dictionary.removeValue(forKey: indexPath.row)
        return
    }

    //store last object in dictionary
    let lastIndex = dictionary[dictionary.count-1]
    
    for i in indexPath.row...dictionary.count-1{
         if i == dictionary.count-2{
             //remove last object in the dictionary at end of loop
             dictionary.removeValue(forKey: dictionary.count-1)
             //append last object
             guard let lastIndex = lastIndex else {return}
             dictionary.updateValue(lastIndex, forKey: dictionary.count-1)
         } else if i < dictionary.count-1{
             //increment all dictionary indices past the deleted row down one
                 let value = dictionary[i+i]
                 guard let value = value else {return}
                 dictionary.updateValue(value, forKey: i)
         }

     }
}

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:

func deleteRows<T>(indexPath: IndexPath, dictionary: inout Dictionary<Int, T>){
    
    if indexPath.row == dictionary.count-1{
        //if its the last object in the tableView, delete and return
        dictionary.removeValue(forKey: indexPath.row)
        return
    }

    //store last object in dictionary
    let lastIndex = dictionary[dictionary.count-1]
    
    for i in indexPath.row...dictionary.count-1{
         if i == dictionary.count-2{
             //remove last object in the dictionary at end of loop
             dictionary.removeValue(forKey: dictionary.count-1)
             //append last object
             guard let lastIndex = lastIndex else {return}
             dictionary.updateValue(lastIndex, forKey: dictionary.count-1)
         } else if i < dictionary.count-1{
             //increment all dictionary indices past the deleted row down one
                 let value = dictionary[i+i]
                 guard let value = value else {return}
                 dictionary.updateValue(value, forKey: i)
         }

     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文