保存 Bool/tableView 复选标记 - 第三次幸运 [英] Saving Bool/ tableView Checkmark - 3rd time lucky

查看:15
本文介绍了保存 Bool/tableView 复选标记 - 第三次幸运的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第三次发布此问题,但尚未得到有效回复.

This is third time I've posted this issues and am yet to get a working response.

So I have a fitness app, the user selected one workout before it been displayed as table view, when a cell is selected I want it to show that cell(containing an exercise) as completed by marking it with a checkmark.这工作正常,但我正在努力解决如何在应用程序终止并重新启动时保存该复选标记.

So I have a fitness app, the user selected one workout before it been displayed as table view, when a cell is selected I want it to show that cell(containing an exercise) as completed by marking it with a checkmark. this works fine but I am struggling with how to save that check mark when the app is terminated and re launched.

下面我给出了一个锻炼模型和表格视图控制器的例子.

Below I have given an example of one of the workout models and the table view controller.

请有人尝试解决这个问题!!

Please can someone try and solve this!!

谢谢.

乔希

锻炼模型示例 -

import Foundation
class The600Workout {
    var workoutArray = [
        Workout(exercise: "Don't forget to warm up before every workout!", completed: false),
        Workout(exercise: "Start with little/ no weight and work your way up", completed: false),
        Workout(exercise: "------------------------------------------------------------------", completed: false),
        Workout(exercise: "Pull ups | 25 Reps", completed: false),
        Workout(exercise: "Lunges | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Calf Raises | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Shoulder press | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Push ups | 50 Reps", completed: false),
        Workout(exercise: "Shrugs | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Leg raises | 50 Reps", completed: false),
        Workout(exercise: "Bench press | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "More Pull ups | 25 Reps", completed: false),
        Workout(exercise: "Squats | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Incline Bench press | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Bicep curls | 50 Reps (Low weight)", completed: false),
        Workout(exercise: "Tricep pull downs | 50 Reps (Low weight)", completed: false),
    ]
}

表视图控制器

import UIKit
class workoutTableView: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var workoutTableView: UITableView!
    var navTitle: String = ""
    var workout = [Workout]()
    let tlabel = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        setWorkout()
        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
        }
        else if navTitle == "5 Days for Muscle" {
            workout = FiveDaysForMuscle().workoutArray
        }
        else if navTitle == "Marathon Ready" {
            workout = MarathonReady().workoutArray
        }
        else if navTitle == "HIIT @ Home" {
            workout = HIITAtHome().workoutArray
        }
        else if navTitle == "Get Strong" {
            workout = GetStrong().workoutArray
        }
        else if navTitle == "Body Weight Blast" {
            workout = BodyWeightBlast().workoutArray
        }
        else if navTitle == "Bands Pump" {
          workout = BandsPump().workoutArray
        }
        else if navTitle == "Quickie Warm up" {
            workout = QuickieWarmUp().workoutArray
        }
        else if navTitle == "The Best Circuit Workout" {
            workout = TheBestCircuit().workoutArray
        }
        else if navTitle == "The Gym HIIT Workout" {

            workout = GymHIIT().workoutArray
        }
        else if navTitle == "The Ultimate Workout" {
            workout = UltimateWorkout().workoutArray
        }
        else if navTitle == "Warm up For Weights" {
            workout = WarmUpForWeights().workoutArray
        }
        else if navTitle == "6 Day Bro Split" {
          workout = SixDayBroSplit().workoutArray
        }
        else if navTitle == "Explosive Workout" {
            workout = ExplosiveWorkout().workoutArray
        }
        else if navTitle == "Strength Circuit" {
            workout = StrengthCircuit().workoutArray
        }
        else if navTitle == "Killer Circuit" {    
            workout = KillerCircuit().workoutArray
        }
        else 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) {
        workout[indexPath.row].completed = !workout[indexPath.row].completed
        tableView.cellForRow(at: indexPath)?.accessoryType = workout[indexPath.row].completed ?  .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].exercise
        cell.accessoryType = workout[indexPath.row].completed ?  .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   
    }
}

推荐答案

将单元格的当前状态(isSelected 或 not)保存到 UserDefault.重新启动后,根据 UserDefaults 数据自动选择单元格.例如:

Save cell's current state(isSelected or not) to UserDefault. After relaunch, select the cell automatically according to UserDefaults data. For ex:

var selectedCell = 0 // For declaration

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        workout[indexPath.row].completed = !workout[indexPath.row].completed
        tableView.cellForRow(at: indexPath)?.accessoryType = workout[indexPath.row].completed ?  .checkmark : .none
        tableView.deselectRow(at: indexPath, animated: false)

        self.selectedCell = indexPath.row 
        UserDefaults.standart.set(self.selectedCell, forKey: "selectedCellIndex")
    }

在 cellForRowAt:

And at cellForRowAt:

if indexPath.row == UserDefaults.standart.object(forKey: selectedCellIndex){
//select this cell and whatever you want
} else {
//Other cells 
}

希望能帮到你...

这篇关于保存 Bool/tableView 复选标记 - 第三次幸运的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆