Tableview 复选标记错误的地方
Tableview checkmark wrong place
我试过在单元格上添加复选标记,但如果我点击第 0 行第 0 行,
第 2 节第 0 行也已检查。另外,如果点击第 0 节第 1 行,第 3 节第 0 行已选中(如果我点击第 2 节第 0 行,则相反,
如果点击第 3 节第 0 行,第 0 节第 1 行,也检查第 0 节第 0 行)。
如果快速滚动,所有检查都会消失。
我怎样才能解决这个问题?提前致谢!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var sectionName = ["Cars", "Motor Cycles", "EV", "Hybrid"]
var cars = ["TOYOTA", "AUDI", "BMW", "HONDA", "VOLVO", "VW","FIAT"]
var motorCycles = ["YAMAHA", "HONDA", "DUCATI", "HARLEY DAVIDSON", "VICTORY"]
var yes = "YES"
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionName.count
}
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
return sectionName[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return cars.count
} else if section == 1 {
return motorCycles.count
} else {
return 1
}
}
let cellId = "cell"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let section = indexPath.section
if section == 0 {
cell.textLabel?.text = cars[indexPath.row]
} else if section == 1 {
cell.textLabel?.text = motorCycles[indexPath.row]
} else {
cell.textLabel?.text = yes
}
return cell
}
var firstArray = [String?]()
var secondArray = [String?]()
var thirdString = ""
var fourthString = ""
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath)
let section = indexPath.section
let text = currentCell!.textLabel!.text!
if currentCell?.accessoryType == .checkmark {
switch section {
case 0:
firstArray = firstArray.filter( { [=10=] != text } )
currentCell?.accessoryType = .none
case 1:
secondArray = secondArray.filter( { [=10=] != text } )
currentCell?.accessoryType = .none
case 2:
thirdString = ""
currentCell?.accessoryType = .none
case 3:
fourthString = ""
currentCell?.accessoryType = .none
default:
break
}
} else {
switch section {
case 0:
firstArray.append(text)
currentCell?.accessoryType = .checkmark
case 1:
secondArray.append(text)
currentCell?.accessoryType = .checkmark
case 2:
thirdString = text
currentCell?.accessoryType = .checkmark
case 3:
thirdString = text
currentCell?.accessoryType = .checkmark
default:
break
}
}
}
}
更好的方法是将 checkMark 设置逻辑放在 cellForRow
中,因为因为出列不需要的单元格可能会被选中,所以在 didSelectRow
中存储 Indexpath/s of one/s 您要检查并重新加载包含所选 IndexPath
的单元格
我试过在单元格上添加复选标记,但如果我点击第 0 行第 0 行, 第 2 节第 0 行也已检查。另外,如果点击第 0 节第 1 行,第 3 节第 0 行已选中(如果我点击第 2 节第 0 行,则相反, 如果点击第 3 节第 0 行,第 0 节第 1 行,也检查第 0 节第 0 行)。
如果快速滚动,所有检查都会消失。 我怎样才能解决这个问题?提前致谢!
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var sectionName = ["Cars", "Motor Cycles", "EV", "Hybrid"]
var cars = ["TOYOTA", "AUDI", "BMW", "HONDA", "VOLVO", "VW","FIAT"]
var motorCycles = ["YAMAHA", "HONDA", "DUCATI", "HARLEY DAVIDSON", "VICTORY"]
var yes = "YES"
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return sectionName.count
}
func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? {
return sectionName[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return cars.count
} else if section == 1 {
return motorCycles.count
} else {
return 1
}
}
let cellId = "cell"
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
let section = indexPath.section
if section == 0 {
cell.textLabel?.text = cars[indexPath.row]
} else if section == 1 {
cell.textLabel?.text = motorCycles[indexPath.row]
} else {
cell.textLabel?.text = yes
}
return cell
}
var firstArray = [String?]()
var secondArray = [String?]()
var thirdString = ""
var fourthString = ""
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath)
let section = indexPath.section
let text = currentCell!.textLabel!.text!
if currentCell?.accessoryType == .checkmark {
switch section {
case 0:
firstArray = firstArray.filter( { [=10=] != text } )
currentCell?.accessoryType = .none
case 1:
secondArray = secondArray.filter( { [=10=] != text } )
currentCell?.accessoryType = .none
case 2:
thirdString = ""
currentCell?.accessoryType = .none
case 3:
fourthString = ""
currentCell?.accessoryType = .none
default:
break
}
} else {
switch section {
case 0:
firstArray.append(text)
currentCell?.accessoryType = .checkmark
case 1:
secondArray.append(text)
currentCell?.accessoryType = .checkmark
case 2:
thirdString = text
currentCell?.accessoryType = .checkmark
case 3:
thirdString = text
currentCell?.accessoryType = .checkmark
default:
break
}
}
}
}
更好的方法是将 checkMark 设置逻辑放在 cellForRow
中,因为因为出列不需要的单元格可能会被选中,所以在 didSelectRow
中存储 Indexpath/s of one/s 您要检查并重新加载包含所选 IndexPath