ios - 设置 UITableView 使用的数组索引的不同方法

标签 ios swift

问题,

与我在下面所做的相比,有没有人有更简洁的解决方案?

设置,

我需要从后端下载加载数据,并按照后端确定的特定顺序将其放入 tableView 中。

问题,

我从后端获得了列表顺序,但顺序被打乱了,因为我正在追加到一个数组。当我尝试添加到特定的 index 时,我会得到 index out of range (当然)因为如果下载的第一个项目应该在 index 6 那么它将超出范围。

我的解决方案,

这是我的解决方案,但我有兴趣了解其他人如何对此进行改进或找到另一种方法来完成同样的事情,

 if self.array.count >= indexNumberFromBackend {
        self.array.insert(data, at: indexNumberFromBackend)
    } else {
        if indexNumber > self.array.count {
            if self.array.count <= indexNumberFromBackend {
               self.array.append(data)
            } else {
               self.array.insert(data, at: indexNumberFromBackend)
            }
        }
    }

最佳答案

你不能只对数组进行排序吗?

关于ios - 设置 UITableView 使用的数组索引的不同方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42673059/

相关文章:

ios - 使用 AVAudioPlayer 播放多个音频文件

ios - 如何在 Objective-C 中创建委托(delegate)?

iphone - 在 App Purchase 中不适用于 iPhone 5 设备

ios - 如果字符串包含某些字符,Swift 会删除扩展字符吗?

ios - 如何在 swift 类中将变量设置为 self(class)? iOS 代码

swift 3 : Inheritance from non-named type

ios - Twitter.sharedInstance() 不存在 swift TwitterKit

ios - 从最后一个已知邮件 ID 获取最新邮件 - Mailcore

ios - 通过 AirDrop 发送自定义文件类型

ios - PhoneGap BarcodeScanner 插件不工作