Swift 如何阻止UITableView重用单元格?
我有一个Swift 如何阻止UITableView重用单元格?,swift,Swift,我有一个UITableView包含大约30个单元格。用户只能选择一个。选定单元格后,它将显示复选标记。但问题是,每当我向下滚动UITableView时,它会显示我从未单击过的单元格,旁边有复选标记。解决这个问题的正确方法是什么 override func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of se
UITableView
包含大约30个单元格。用户只能选择一个。选定单元格后,它将显示复选标记。但问题是,每当我向下滚动UITableView
时,它会显示我从未单击过的单元格,旁边有复选标记。解决这个问题的正确方法是什么
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return breedNameArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Bcell")
let cell = tableView.dequeueReusableCell(withIdentifier: "Bcell", for: indexPath)
//cell.accessoryType = UITableViewCell.AccessoryType.checkmark
cell.textLabel?.text = breedNameArray[indexPath.item]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
tableView.register(UINib(nibName: "BBcell", bundle: nil), forCellReuseIdentifier: "Cell")
//let cell = tableView.dequeueReusableCell(withIdentifier: "Breeds", for: indexPath)
//tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Bcell")
//let cell = tableView.dequeueReusableCell(withIdentifier: "Breeds", for: indexPath)
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
滚动时看到单元格得到复选标记的原因是,cellForRowAt中对tableView.dequeueReusableCell(标识符为:“Bcell”,for:indexPath)的调用重用了已分配的标识符为“Bcell”的UITableViewCell对象。以这种方式重用时,UITableView不会自动重置重用的UITableViewCell对象的属性 由于一次只允许选择一个UITableViewCell,一个简单的解决方案是添加一个局部变量来跟踪选定的IndexPath,然后使用局部变量在cellForRowAt中相应地设置accessoryType。下面的代码说明了我的意思:
// The local variable to keep track of the selected IndexPath
var selectedIndexPath = IndexPath()
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return breedNameArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Bcell")
let cell = tableView.dequeueReusableCell(withIdentifier: "Bcell", for: indexPath)
// Use selectedIndexPath to set the accessoryType as .checkmark or .none
if indexPath == selectedIndexPath {
cell.accessoryType = UITableViewCell.AccessoryType.checkmark
} else {
cell.accessoryType = UITableViewCell.AccessoryType.none
}
cell.textLabel?.text = breedNameArray[indexPath.item]
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set the selectedIndexPath
selectedIndexPath = indexPath
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
tableView.register(UINib(nibName: "BBcell", bundle: nil), forCellReuseIdentifier: "Cell")
//let cell = tableView.dequeueReusableCell(withIdentifier: "Breeds", for: indexPath)
//tableView.deselectRow(at: tableView.indexPathForSelectedRow!, animated: true)
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
// Un-Set the selectedIndexPath
selectedIndexPath = IndexPath()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Bcell")
//let cell = tableView.dequeueReusableCell(withIdentifier: "Breeds", for: indexPath)
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}