【发布时间】: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