我正在开发一个基于健身的应用程序,用户将选择他们的锻炼,然后进入包含所选锻炼的锻炼的表格 View 。当用户点击单元格时,我希望出现一个复选标记以指示练习已完成。我已经成功做到这一点,但现在我想保留复选标记,因此如果用户关闭应用程序,那么当它重新打开并再次选择锻炼时,复选标记仍然会显示。
任何帮助将不胜感激。
表格 View 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
}
}
最佳答案
我要做的是创建某种包含某种值的锻炼列表(可能是稍后 IndexPath 的索引)。然后将 NSArray 或 NSDictionary 存储在 UserDefaults 中
关于ios - 保留 Swift 复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59687353/