我有一个使用相同单元类的多个单元格的表。这个类包括一个UISwitch。
我的表有两个部分。第一部分只有6个单元格,但第二部分有11个单元格。
这个神奇的数字11有一些东西在我的应用程序中引起奇怪的行为。每当我切换第二部分中的第一个开关时,第11个开关也会切换。如果我切换第11个开关,第一个用它自动切换,也是如此。第二部分中的第一个和第11个细胞是唯一表现出这种行为的细胞。
我假设它与索引路径号有关,但我不知道从哪里开始。这是我的代码:
import UIKit
class ScoringSetupViewController: UIViewController, UITableViewDataSource,
UITableViewDelegate, SwitchChangedDelegate {
@IBOutlet weak var scoringCategoriesTableView: UITableView!
let primaryCategories:[String] = ["Average Headshot Kills", "Average Kill Place", "Average Win Place", "Total Steak Dinners", "Total Chicken Dinners", "Average Total Kills"]
let secondaryCategories:[String] = ["Average Assists","Highest Assists","Average Damage Delt", "Highest Damage Delt", "Total Kill Streaks", "Highest Total Kills", "Highest Headshot Kills", "Longest Distance Kill", "Average Revives", "Total Road Kills", "Average Time Survived"]
var acceptedPrimaryCategories:[String:Bool] = ["Average Headshot Kills":true, "Average Kill Place":true, "Average Win Place":true, "Total Steak Dinners":true, "Total Chicken Dinners":true, "Average Total Kills":true]
var acceptedSecondaryCategories:[String:Bool] = ["Average Assists":true,"Highest Assists":true,"Average Damage Delt":true, "Highest Damage Delt":true, "Total Kill Streaks":true, "Highest Total Kills":true, "Highest Headshot Kills":true, "Longest Distance Kill":true, "Average Revives":true, "Total Road Kills":true, "Average Time Survived":true]
override func viewDidLoad() {
super.viewDidLoad()
scoringCategoriesTableView.dataSource = self
scoringCategoriesTableView.delegate = self
}
func changeStateTo(isOn: Bool, section: Int, row: Int) {
if section == 0 {
if row == 0 {
acceptedPrimaryCategories["Average Headshot Kills"] = isOn
} else if row == 1 {
acceptedPrimaryCategories["Average Kill Place"] = isOn
} else if row == 2 {
acceptedPrimaryCategories["Average Win Place"] = isOn
} else if row == 3 {
acceptedPrimaryCategories["Total Steak Dinners"] = isOn
} else if row == 4 {
acceptedPrimaryCategories["Total Chicken Dinners"] = isOn
} else if row == 5 {
acceptedPrimaryCategories["Average Total Kills"] = isOn
}
print(acceptedPrimaryCategories)
} else if section == 1 {
if row == 0 {
acceptedSecondaryCategories["Average Assists"] = isOn
} else if row == 1 {
acceptedSecondaryCategories["Highest Assists"] = isOn
} else if row == 2 {
acceptedSecondaryCategories["Average Damage Delt"] = isOn
} else if row == 3 {
acceptedSecondaryCategories["Highest Damage Delt"] = isOn
} else if row == 4 {
acceptedSecondaryCategories["Total Kill Streaks"] = isOn
} else if row == 5 {
acceptedSecondaryCategories["Highest Total Kills"] = isOn
} else if row == 6 {
acceptedSecondaryCategories["Highest Headshot Kills"] = isOn
} else if row == 7 {
acceptedSecondaryCategories["Longest Distance Kill"] = isOn
} else if row == 8 {
acceptedSecondaryCategories["Average Revives"] = isOn
} else if row == 9 {
acceptedSecondaryCategories["Total Road Kills"] = isOn
} else if row == 10 {
acceptedSecondaryCategories["Average Time Survived"] = isOn
}
print(acceptedSecondaryCategories)
}
}
@IBAction func continueButtonTapped(_ sender: Any) {
}
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Primary Scoring Categories"
} else if section == 1 {
return "Secondary Scoring Cagtegories"
} else {
return ""
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return primaryCategories.count
} else if section == 1 {
return secondaryCategories.count
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scoringCategoryCell", for: indexPath) as! ScoringCategoryTableViewCell
cell.delegate = self
cell.section = indexPath.section
cell.row = indexPath.row
if indexPath.section == 0 {
cell.categoryNameLabel.text = primaryCategories[indexPath.row]
} else if indexPath.section == 1 {
cell.categoryNameLabel.text = secondaryCategories[indexPath.row]
}
return cell
return UITableViewCell()
}
}
表格单元自定义类
import UIKit
protocol SwitchChangedDelegate {
func changeStateTo(isOn: Bool, section: Int, row: Int)
}
class ScoringCategoryTableViewCell: UITableViewCell {
@IBOutlet weak var categoryNameLabel: UILabel!
@IBOutlet weak var categorySwitch: UISwitch!
var delegate: SwitchChangedDelegate?
var section: Int?
var row: Int?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBAction func switchToggled(_ sender: UISwitch) {
if sender.isOn {
self.delegate?.changeStateTo(isOn: true, section: section!, row: row!)
} else {
self.delegate?.changeStateTo(isOn: false, section: section!, row: row!)
}
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
答案 0 :(得分:1)
原因是重复使用单元格,所以在cellForRow
中你必须处理
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "scoringCategoryCell", for: indexPath) as! ScoringCategoryTableViewCell
cell.delegate = self
cell.section = indexPath.section
cell.row = indexPath.row
cell.categorySwitch.isOn = // value
}
//
也在这里
func changeStateTo(isOn: Bool, section: Int, row: Int)
紧凑的发送
func changeStateTo(isOn: Bool, indexPath:IndexPath)
或者
func changeStateTo(cell:ScoringCategoryTableViewCell) // ask cell for it's indexPath and then access all it's current properties
以一种使用subscript []访问值的方式构建模型,而不是通过检查字典的值
//
这里
return cell
return UITableViewCell()
第二行无用