我有一个问题,您认为解决我的问题的最佳方法是什么。
情况是,我的模型非常大并且包含大量数据。这些信息存储在数据库中并发送到客户端。现在我想以表格 View 的方式显示这些数据。显示数据不是问题,但访问数据的最佳方式才是问题。
我的问题是:在表格 View 单元格中处理大数据模型的明智方法是什么
举个例子,我有一个包含 30 个变量的模型。 现在我有一个页面 View Controller ,其中有一个 TableView ,在第一页上我想显示变量 1,2,5,6,7,9,10 和下一页 1,2,21,23,24 ,22(共 100 个对象),页面上方有一个小指示符,表示数字或字符串的含义。
所以我想到了使用 Swift 4 的解决方案
typealias listTypeObject = (name: String, minSize: Int, object: KeyPath<Any, Any>)
这里的一个问题是您无法转换关键路径类型,因此它无法接受我的关键路径。所以这至少在这种状态下是行不通的。如果有人知道这个问题的解决方案那就太棒了!
另一个解决方案是手动创建每个列表,并将标题和内容分开。这将花费我很多行代码,我认为这些代码行可以少得多。
我希望有人可以向我展示更好的方法,或者只是告诉我最好的方法是手动。
非常感谢!
更新 1: 代码示例//这是包含页面的列表
enum listTypes {
case player
case playerPoints
case team
}
// I have a base model
class BaseModel {
var id: Int = 0
let updated: Date = Date()
init(id: Int) {
self.id = id
}
}
// I have multiple final models like this but bigger
final class PlayerModel: BaseModel {
var firstName: String = "John"
var surname: String = "Doe"
var pointsInFootball: Int = 60
var pointsInBasketball: Int = 23
init(id: Int, firstName: String, surname: String, pointsInFootball: Int, pointsInBasketball: Int) {
super.init(id: id)
self.firstName = firstName
self.surname = surname
self.pointsInFootball = pointsInFootball
self.pointsInBasketball = pointsInBasketball
}
}
final class TeamModel: BaseModel {
var name: String = "TeamTest"
}
// Lets create the example
var dataArray: [BaseModel] = []
// Add some test objects to the array
for i in 0..<10 {
let object = PlayerModel(id: i, firstName: "John\(i)", surname: "Doe\(i)", pointsInFootball: 10 * i, pointsInBasketball: 5 * i)
dataArray.append(object)
}
// So now in the list I would like to say what is accessible for the table view
// listTypes.player = PlayerModel: firstName - surname - age
// listTypes.playerPoints = PlayerModel: firstName - pointsInFootball - pointsInBasketball - pointsInSkiing
// But list type team should be
// listTypes.team = TeamModel: name
最佳答案
据我了解,我认为你应该使用枚举。通过使用枚举数组,它可以隔离您想要呈现的变量,并且您不必创建唯一的 tableView 来完成此操作。您只需将一个数组传递给 tableView delegate/datasouce 函数。没有代码示例/更多信息,这就是我所能推断的。
关于ios - 大模型 TableView 解决方案 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44914312/