将复选标记保存到 tableView Swift 时出现问题

Posted

技术标签:

【中文标题】将复选标记保存到 tableView Swift 时出现问题【英文标题】:Problem saving checkmark to tableView Swift 【发布时间】:2020-01-18 18:27:05 【问题描述】:

我在将复选标记保存到我的表格视图时遇到问题,本质上它是一个健身应用程序,当用户选择锻炼时,表格视图会显示它,按下锻炼时会显示复选标记,但我无法将该复选标记保存到所需的练习中。 下面我以其中一项锻炼为例和 tableView 控制器。

我之前已发布过此内容,但尚未收到任何有效的回复,因此我们将不胜感激。

其中一项锻炼的示例:

import Foundation

class The600Workout 


    let workoutArray = [

                        "Don't forget to warm up before every workout!",
                        "Start with little/ no weight and work your way up",
                        "--------------------------------------------------------------------------------",
                        "Pull ups | 25 Reps",
                        "Lunges | 50 Reps (Low weight)",
                        "Calf Raises | 50 Reps (Low weight)",
                        "Shoulder press | 50 Reps (Low weight)",
                        "Push ups | 50 Reps",
                        "Shrugs | 50 Reps (Low weight)",
                        "Leg raises | 50 Reps",
                        "Bench press | 50 Reps (Low weight)",
                        "More Pull ups | 25 Reps",
                        "Squats | 50 Reps (Low weight)",
                        "Incline Bench press | 50 Reps (Low weight)",
                        "Bicep curls | 50 Reps (Low weight)",
                        "Tricep pull downs | 50 Reps (Low weight)"]

tableView 控制器:

import UIKit

class workoutTableView: UIViewController, UITableViewDataSource, UITableViewDelegate 

    @IBOutlet weak var workoutTableView: UITableView!

    var navTitle: String = ""
    var workout = [String]()
    let tlabel = UILabel()
    var completed: [Bool] = []

    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
        completed = [Bool](repeating: false, count: workout.count)
    

    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) 

        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
    

谢谢!

【问题讨论】:

您的锻炼模型似乎是一个字符串数组。您将需要您的模型是一个结构数组,其中包含一个字符串和一个布尔值,以便您可以跟踪每个项目的检查状态。 请查看此链接 - ***.com/questions/59605872/… 我已经实现了这个,但这并不能解决保存它的需要。保存很好,但加载当前状态是个问题。 【参考方案1】:

更正:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    completed[indexPath.row] = !completed[indexPath.row]
    tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark

还要加上:

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) 
    completed[indexPath.row] = !completed[indexPath.row]
    tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none

【讨论】:

以上是关于将复选标记保存到 tableView Swift 时出现问题的主要内容,如果未能解决你的问题,请参考以下文章

清除搜索结果后,Swift Tableview 搜索结果单元格选择复选标记未保存

将复选标记图像添加到多个按钮数组

一旦我单击第一个复选框,它也会将复选标记放在另一个复选框中

ios swift:Tableview“un”-复选标记单元格

在 NSUserDefaults 中保存复选标记 - 动态 TableView

UITableView 中需要多次点击以进行复选标记