【发布时间】:2020-01-10 18:44:18
【问题描述】:
我正在开发一个基于健身的应用程序,用户将选择他们的锻炼,然后转到包含所选锻炼锻炼的表格视图。当用户点击单元格时,我希望出现一个复选标记以指示练习已完成。我已经成功了,但现在我想保留复选标记,所以如果用户关闭应用程序,那么当它重新打开并再次选择锻炼时,复选标记仍会显示。
任何帮助将不胜感激。
下面是表格视图VC,
提前谢谢你。
import UIKit
class workoutTableView: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var workoutTableView: UITableView!
var navTitle: String = ""
var workout = [String]()
let tlabel = UILabel()
var completed: [Bool] = []
var saveString = String()
var saveBool = Bool()
override func viewDidLoad() {
super.viewDidLoad()
setWorkout()
completed = [Bool](repeating: false, count: workout.count)
workoutTableView.delegate = self
workoutTableView.dataSource = self
tlabel.text = navTitle
tlabel.textAlignment = .center
tlabel.font = UIFont(name: "Arial Rounded MT Bold", size: 30)
tlabel.adjustsFontSizeToFitWidth = true
navigationItem.titleView = tlabel
}
func setWorkout() {
if navTitle == "The 600 Workout" {
workout = The600Workout().workoutArray
}
if navTitle == "5 Days for Muscle" {
workout = FiveDaysForMuscle().workoutArray
}
if navTitle == "Marathon Ready" {
workout = MarathonReady().workoutArray
}
if navTitle == "HIIT @ Home" {
workout = HIITAtHome().workoutArray
}
if navTitle == "Get Strong" {
workout = GetStrong().workoutArray
}
if navTitle == "Body Weight Blast" {
workout = BodyWeightBlast().workoutArray
}
if navTitle == "Bands Pump" {
workout = BandsPump().workoutArray
}
if navTitle == "Quickie Warm up" {
workout = QuickieWarmUp().workoutArray
}
if navTitle == "The Best Circuit Workout" {
workout = TheBestCircuit().workoutArray
}
if navTitle == "The Gym HIIT Workout" {
workout = GymHIIT().workoutArray
}
if navTitle == "The Ultimate Workout" {
workout = UltimateWorkout().workoutArray
}
if navTitle == "Warm up For Weights" {
workout = WarmUpForWeights().workoutArray
}
if navTitle == "6 Day Bro Split" {
workout = SixDayBroSplit().workoutArray
}
if navTitle == "Explosive Workout" {
workout = ExplosiveWorkout().workoutArray
}
if navTitle == "Strength Circuit" {
workout = StrengthCircuit().workoutArray
}
if navTitle == "Killer Circuit" {
workout = KillerCircuit().workoutArray
}
if navTitle == "Fitness Test" {
workout = FitnessTest().workoutArray
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return workout.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
completed[indexPath.row] = !completed[indexPath.row]
tableView.cellForRow(at: indexPath)?.accessoryType = completed[indexPath.row] ? .checkmark : .none
tableView.deselectRow(at: indexPath, animated: false)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeCell", for: indexPath)
cell.textLabel?.text = workout[indexPath.row]
cell.accessoryType = completed[indexPath.row] ? .checkmark : .none
cell.layer.borderWidth = 5
cell.layer.cornerRadius = 20
cell.layer.borderColor = #colorLiteral(red: 0, green: 0.3285208941, blue: 0.5748849511, alpha: 1)
cell.textLabel?.textColor = UIColor.black
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = .boldSystemFont(ofSize: 15)
return cell
}
}
【问题讨论】:
-
请忽略保存字符串并保存布尔值,它们是我失败尝试的一部分:(
-
实际上,您必须为每种类型分别保存所选锻炼的数组。这不适用于将所选行保留在一个数组中的方式。我建议将整个锻炼数据保存在像 Core Data 这样的数据库中。再一次,一堆连续的
if表达式(没有else)效率极低。请注意,即使navTitle是"The 600 Workout",也会对所有类型进行不必要的检查 -
谢谢,我修改了 if/else if 语句。关于持久性,当您说将它们保存到数据库中时,您是指整个模型还是只是类型?再次感谢
-
我的意思是整个模型