Swift tableView 复选标记在滚动时重复 [英] Swift tableView checkmark repeating on scroll
问题描述
我对 swift 和开发还很陌生,我正在寻求帮助来解决我无法解决的问题.
I'm fairly new to swift and developing, I'm looking for some help with a problem I can't get past.
所以基本上我有一堆自定义类的详细说明锻炼,我使用这些锻炼来填充表格视图以向用户显示所选特定锻炼中的锻炼列表.
So essentially I have a bunch of custom class's which detail workouts, I use those workouts to populate the table view to show the user a list of exercises in the chosen particular workout.
我希望能够在完成后在表格视图中的练习旁边放置一个复选标记,我遇到的问题是当我滚动时复选标记重复,我现在已将其删除以用于新单元格,但这会导致当我向下滚动然后再向上滚动时,选中标记,我明白这是因为我正在重用单元格.我不知道如何解决它,我尝试了各种解决方案,但都没有奏效.
I want to be able to place a checkmark next to an exercise within the table view once it has been completed, the issue I am having is the checkmark repeats when I scroll, I have now removed it for new cells but this causes the checkmark to go when I scroll down and then back up, I understand this is because I am reusing the cell. What I can't figure out is how to fix it, I have tried all sorts of solutions and none have worked.
任何建议将不胜感激!
代码如下,谢谢!
附言为了清楚起见,navTitle 是从之前的 VC 传入的.
p.s. Just for clarity, the navTitle is getting passed in from the previous VC.
import UIKit
class workoutTableView: UIViewController, UITableViewDataSource, UITableViewDelegate {
var navTitle: String = ""
var workout = [String]()
let tlabel = UILabel()
@IBOutlet weak var workoutTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
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 tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
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
}
return workout.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
} else {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
tableView.deselectRow(at: indexPath, animated: false)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "prototypeCell", for: indexPath)
if navTitle == "The 600 Workout" {
workout = The600Workout().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "5 Days for Muscle" {
workout = FiveDaysForMuscle().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Marathon Ready" {
workout = MarathonReady().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "HIIT @ Home" {
workout = HIITAtHome().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Get Strong" {
workout = GetStrong().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Body Weight Blast" {
workout = BodyWeightBlast().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Bands Pump" {
workout = BandsPump().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Quickie Warm up" {
workout = QuickieWarmUp().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "The Best Circuit Workout" {
workout = TheBestCircuit().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "The Gym HIIT Workout" {
workout = GymHIIT().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "The Ultimate Workout" {
workout = UltimateWorkout().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Warm up For Weights" {
workout = WarmUpForWeights().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "6 Day Bro Split" {
workout = SixDayBroSplit().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Explosive Workout" {
workout = ExplosiveWorkout().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Strength Circuit" {
workout = StrengthCircuit().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Killer Circuit" {
workout = KillerCircuit().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
if navTitle == "Fitness Test" {
workout = FitnessTest().workoutArray
cell.textLabel?.text = workout[indexPath.row]
}
cell.accessoryType = .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
}
}
推荐答案
首先用标志制作一个 Workout 的类/结构
First make a class/struct of Workout with a flag
struct Workout {
let name: String
let isComplete: Bool
}
制作示例数据模型
var workouts = [
Workout(name: "Squats", isComplete: false),
Workout(name: "Burpees", isComplete: false),
Workout(name: "Crunches", isComplete: true),
Workout(name: "Push Ups", isComplete: false),
Workout(name: "Jumping Jacks", isComplete: true),
Workout(name: "High Knees", isComplete: false),
Workout(name: "Lunges", isComplete: false),
Workout(name: "Plank", isComplete: false),
Workout(name: "Sechigh Knees", isComplete: false),
Workout(name: "Tricep Dips", isComplete: false),
Workout(name: "Mountain Climbers", isComplete: true),
Workout(name: "Wall Sit", isComplete: true),
Workout(name: "Squats 2", isComplete: false),
Workout(name: "Burpees 2", isComplete: false),
Workout(name: "Crunches 2", isComplete: true),
Workout(name: "Push Ups 2", isComplete: false),
Workout(name: "Jumping Jacks 2", isComplete: false),
Workout(name: "High Knees 2", isComplete: false),
Workout(name: "Lunges 2", isComplete: false),
Workout(name: "Plank 2", isComplete: false),
Workout(name: "Sechigh Knees 2", isComplete: true),
Workout(name: "Tricep Dips 2", isComplete: false),
Workout(name: "Mountain Climbers 2", isComplete: false),
Workout(name: "Wall Sit 2", isComplete: false),
]
使用锻炼类型变量
class CustomCell: UITableViewCell {
var workout: Workout? {
didSet {
guard let workout = workout else { return }
self.textLabel?.text = workout.name
if workout.isComplete {
self.accessoryType = .checkmark
}
else {
self.accessoryType = .none
}
}
}
}
然后在tableView:cellForRowAtIndexPath:
方法中传递变量
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomCell
cell.workout = workouts[indexPath.row]
return cell
}
这篇关于Swift tableView 复选标记在滚动时重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!