我想在表加载时预选单元格。我已遵循此答案,但无法正常工作。它并不总是预先选择正确的单元格。https://stackoverflow.com/a/19296095/12114641它仅选择最后一个单元格。如何使其正常工作?
Tableview设置为单一选择。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell else {
fatalError("Can't find cell")
}
cell.title = dataSource[indexPath.row]
if globalCheckedArray[indexPath.row].checked {
self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
} else {
self.tableView.deselectRow(at: indexPath, animated: false)
}
return cell
}
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
globalCheckedArray[indexPath.row].checked = true
if tableView.indexPathsForSelectedRows?.contains(indexPath) ?? false {
tableView.deselectRow(at: indexPath, animated: true)
return nil
}
return indexPath
}
func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? {
return nil
}
您需要做几件事...
.accessoryType
中的单元格设置cellForRowAt
这里是一个非常简单的例子。在您的情节提要中,添加一个UITableViewController
,将其自定义类设置为TestTableViewController
,然后将其嵌入到UINavigationController
中。
[运行时,您可以选择-即添加复选标记-多行。
当您点击导航栏中的“测试”按钮时,它将被推至SampleViewController
,它将在标签中显示所选的行标题。
当您“返回”时,您的数据将被保留,正确的行将带有复选标记。
struct MyDataStruct {
var title: String = ""
var isSelected: Bool = false
}
class TestTableViewController: UITableViewController {
var myData: [MyDataStruct] = [
MyDataStruct(title: "One", isSelected: false),
MyDataStruct(title: "Two", isSelected: false),
MyDataStruct(title: "Three", isSelected: false),
MyDataStruct(title: "Four", isSelected: false),
MyDataStruct(title: "Five", isSelected: false),
MyDataStruct(title: "Six", isSelected: false),
MyDataStruct(title: "Seven", isSelected: false),
MyDataStruct(title: "Eight", isSelected: false),
MyDataStruct(title: "Nine", isSelected: false),
MyDataStruct(title: "Ten", isSelected: false),
MyDataStruct(title: "Eleven", isSelected: false),
MyDataStruct(title: "Twelve", isSelected: false),
MyDataStruct(title: "Thirteen", isSelected: false),
MyDataStruct(title: "Fourteen", isSelected: false),
MyDataStruct(title: "Fifteen", isSelected: false),
MyDataStruct(title: "Sixteen", isSelected: false),
MyDataStruct(title: "Seventeen", isSelected: false),
MyDataStruct(title: "Eighteen", isSelected: false),
MyDataStruct(title: "Nineteen", isSelected: false),
MyDataStruct(title: "Twenty", isSelected: false),
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "TableViewCell")
let b = UIBarButtonItem(title: "Test", style: .plain, target: self, action: #selector(barButtonTapped(_:)))
self.navigationItem.rightBarButtonItem = b
}
@objc func barButtonTapped(_ sender: Any?) -> Void {
// instantiate the next view controller
let vc = SampleViewController()
// filter myData to only the elements with isSelected = true
let selectedItems = myData.filter{ $0.isSelected == true }
// set the next VC's property (i.e. pass the data to it)
vc.theSelectedRows = selectedItems
// push to the next VC
self.navigationController?.pushViewController(vc, animated: true)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
let dataElement: MyDataStruct = myData[indexPath.row]
cell.textLabel?.text = dataElement.title
cell.accessoryType = dataElement.isSelected ? .checkmark : .none
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// toggle selected state in data
myData[indexPath.row].isSelected = !myData[indexPath.row].isSelected
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
class SampleViewController: UIViewController {
var theSelectedRows: [MyDataStruct] = [MyDataStruct]()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let resultLabel: UILabel = UILabel()
view.addSubview(resultLabel)
resultLabel.translatesAutoresizingMaskIntoConstraints = false
resultLabel.numberOfLines = 0
var s = ""
theSelectedRows.forEach { d in
s += d.title + "\n"
}
resultLabel.text = s
resultLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
resultLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
输出: