【发布时间】:2018-05-15 22:02:43
【问题描述】:
我正在尝试计算领域列表(包含在“WorkoutSessionObject”中)中的练习数量,以填充表格视图的正确行数,但由于某种原因,我无法计算出它不让我可以访问该物业吗?
它肯定是在检索 WorkoutSessionObject,因为我已经尝试过打印它。
代码如下:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//filter for a specific workout
let predicate = NSPredicate(format: "workoutID = %@", workoutID)
let workoutExercises = realm.objects(WorkoutSessionObject.self).filter(predicate)
//access the 'exercises' list and count it - this isn't working?
let numberOfExercises = workoutExercises.exercises.count
return numberOfExercises
}
我已经以类似的方式访问属性来填充单元格,但我显然在那里使用 index.row。
我得到的错误是
“结果”类型的值没有成员“练习”
在代码中标记为不工作的行上
研究这里有一个答案Retrieve the List property count of realm for tableview Swift,但这似乎不会返回范围内的计数(它允许在示例中打印,但这在我的代码中不起作用)
即我也试过这个,它不起作用,因为在范围之外无法访问计数:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let predicate = NSPredicate(format: "workoutID = %@", workoutID)
//the below works to count but then not accessible outside scope to define number of rows :-(
let workoutExercises = realm.objects(WorkoutSessionObject.self).filter(predicate)
for exercises in workoutExercises {
let exerciseCount = exercises.exercises.count
return exerciseCount
}
//return exercise count for table rows - this doesn't work because not in scope
return exerciseCount
}
有什么想法吗?
【问题讨论】:
-
你正在从你的'for'循环中返回,这不是你应该做的。如果你想知道你有多少练习,你需要增加计数,然后返回最终的计数。
标签: ios swift uitableview realm swift4