【发布时间】:2020-12-19 03:37:23
【问题描述】:
目标:在自定义 TableViewCell 中创建一个“添加到收藏夹”按钮,然后将收藏夹列表保存到 UserDefaults 中,这样我们就可以只获得收藏夹的表格视图。 (我使用 segmentedControl 作为过滤 libraryFilter = 0(Plan) / 1 = Tag / 2 = Duration / 3 = Favorite 的方法)
jsonErgWorkouts 是来自 JSON 文件的锻炼数组,它是过滤的基础。
这是我编写的代码。
自定义 TableViewCell:
import UIKit
class ErgWorkoutCell: UITableViewCell {
@IBOutlet weak var ergWorkoutTitle: UILabel!
}
在 TableView 视图控制器中
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let selectedGroup = selectedWorkoutGroup(libraryFilter: libraryFilter, jsonErgWorkouts: jsonErgWorkouts, workoutGroupBox: workoutGroupBox)
/// this is cast AS! ErgWorkoutCell since this is a custom tableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "ergWorkoutCell") as! ErgWorkoutCell
cell.ergWorkoutTitle.text = selectedGroup[indexPath.row].title
return cell
}
}
func selectedWorkoutGroup(libraryFilter: Int, jsonErgWorkouts:[workoutList], workoutGroupBox: UITextField) -> [workoutList] {
var selectedGroup = [workoutList]()
if libraryFilter == 0 { // Segmented Control #1 (Plan)
selectedGroup = jsonErgWorkouts.filter { $0.group == workoutGroupBox.text }
/// code for libraryFilter 1(Tag)
/// and libraryFilter 2(Duration)
} else if libraryFilter == 3 {
// Need code here to return the filtered JSON of favorites only
}
return selectedGroup
}
【问题讨论】:
-
你有几个无法解释的变量。
标签: swift uitableview userdefaults