我目前有两个字典数组来存储数据:
var workoutData = [String:[String:[String:Int]]]()
和
var workoutSets = [String:[String:Int]]()
当在该行的末尾选中复选框时,我将数据分配给workoutSets,然后将workoutSets
数据整体分配给workoutData
:
workoutSets["Set \(indexPath.row)"] = ["Weight":Int(weightLifted)!, "Reps":Int(repsPerformed)!]
workoutData[exercise] = workoutSets
这适用于第一部分数据,但是当我将数据输入UITable
中下一部分的第一行并检查复选框时,其他数据行的值会被转发。我添加了一个屏幕截图,以便您可以看到我面临的情况。
打印时,workoutData
看起来像
["Cable Row": ["Set 2": ["Weight": 10, "Reps": 12],"Set 3": ["Weight": 28, "Reps": 8],"Set 1": ["Weight": 45, "Reps": 5]],
"Arnold Press": ["Set 2": ["Weight": 10, "Reps": 12], "Set 3": ["Weight": 28, "Reps": 8], "Set 1": ["Weight": 10, "Reps": 12]]]
是否有更好的方法来存储此数据,因为我在加载表时也使用workoutData
方法中的cellForRowAt
,以便在加载新单元格时不会重复使用单元格数据
答案 0 :(得分:1)
对于存储具有已定义属性和属性的结构化对象的数据,将其定义为从NSObject
继承的对象更有意义。
对于此自定义对象,您可以将所需的属性定义为" weight" &安培; "代表&#34 ;.还要创建类Set
的复合对象,您可以在其中添加对象数组。鉴于下面的对象层次结构可能会有所帮助。
public class Workout {
var displayName:String?
var sets:[Set]?
var key:String?
}
public class Set {
var weight:Int?
var reps:Int?
}
var workouts = [Workout]()
var workout1 = Workout()
workout1.displayName = "Arnold Press"
workout1.sets = [Set]()
var set1 = Set()
set1.reps = 10
set1.weight = 80
workout1.sets?.append(set1)
var set2 = Set()
set2.reps = 12
set2.weight = 80
workout1.sets?.append(set2)
var workout2 = Workout()
workout2.displayName = "Cable Row"
workout2.sets = [Set]()
var set11 = Set()
set11.reps = 10
set11.weight = 80
workout1.sets?.append(set11)
var set22 = Set()
set22.reps = 12
set22.weight = 80
workout2.sets?.append(set22)
workouts.append(workout1)
workouts.append(workout2)