【问题标题】:How to save position of reordered cells in Collection View after I restart the app? Swift重新启动应用程序后,如何在集合视图中保存重新排序的单元格的位置?迅速
【发布时间】:2018-01-18 00:48:51
【问题描述】:

当我尝试使用这两种方法重新排序 UICollectionView 中单元格的位置时:

var teams: [Team]?

override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
    return true
}

override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {

    if let temp = teams?[sourceIndexPath.item] {
        teams?[sourceIndexPath.item] = (teams?[destinationIndexPath.item])!
        teams?[destinationIndexPath.item] = temp
    }

    print("Starting Index: \(sourceIndexPath.item)")
    print("Ending Index: \(destinationIndexPath.item)")
}

它工作正常但是在重新启动我的应用程序后我想保存重新排序的单元格的位置。

你能推荐我哪种方法?

附加信息:

“teams”数组存储的是 Team 类的对象:

class Team: NSObject {

    var id: String?
    var name: String?
    var logo: String?
    var players: [Player]?
}

class Player: NSObject {

    var alias: String?
    var name: String?
    var age: String?
    var country: String?
    var imageName: String?
    var info: Info?
}

class Info: NSObject {

    var screenshots: [String]?
    var bio: String?
    var gear: Gear?
    var povs: [String]?
    var cfg: Config?
}

class Gear: NSObject {

    var monitor: String?
    var mouse: String?
    var mousepad: String?
    var keyboard: String?
    var headset: String?
}

class Config: NSObject {

    var mouseSettings: [String]?
    var monitorSettings: [String]?
    var crosshaircfg: [String]?
}

提前感谢您的帮助!

【问题讨论】:

  • 我想你可以使用核心数据来达到这个目的,当你重启你的应用程序时,从 coredata 中获取重新排序的记录并显示在屏幕上
  • 您应该保存单元格的唯一标识符而不是其顺序,例如 team.id

标签: ios swift core-data uicollectionview uicollectionviewcell


【解决方案1】:

我会为此使用 UserDefaults。

我将位置作为整数存储在 userDefaults 中,并使用项目名称作为键。

如何存储位置

func saveReorderedArray() {
    for (index, item) in yourArray.enumerated() {
        let position = index + 1
        UserDefaults.standard.set(position, forKey: item.name)
    }
}

在应用程序启动时,我调用 reorderArray 函数来获取位置并将其存储在字典数组中,使用项目名称作为键。

如何检索职位

func reorderArray() {
    var items: [[String: Int]] = []

    // Get the positions
    for item in yourArray {
        var position = UserDefaults.standard.integer(forKey: item.name)

        // If a new item is added, set position to 999
        if position == 0 {
            position = 999
        }

        items.append([itemName : position])
    }

    for item in items {
        // Get position from dictionary
        let position = Array(item.values)[0]
        let itemName = Array(item.keys)[0]

        // Get index from yourArray
        let index = yourArray.index { (item) -> Bool in
            item.name == itemName
        }

        // Arrange the correct positions for the cells
        if let i = index {
            let m = yourArray.remove(at: i)

            // Append to last position of the array
            if position == 999 {
                yourArray.append(m)
            } 
            else {
                yourArray.insert(m, at: position - 1)  // Insert at the specific position
            }
        }
    }
}

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多