问题
Asking because all solutions available are for Swift 3. Also, I am dealing with a problem that when I scroll up and down after highlighting + selecting a row, new rows that appear with the same indexPath (after scrolling) will also be checked (but not highlighted).
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
}
}
回答1:
Instead of change accessoryType
on didSelectRowAt
and didDeselectRowAt
methods, you should override and do it on setSelected(_:animated:) from your cell class.
class YourCellClass: UITableViewCell {
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
if selected {
accessoryType = .checkmark
} else {
accessoryType = .none
}
}
}
回答2:
You can possibly Use my Way:
Take a button inside a cell and use it like this:
var arrSelectedRows:[Int] = []
//MARK:- UITableView Delegate Methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrCategoryData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier : String = "MenuPopupTBCell";
var cell : MenuPopupTBCell! = tblMenuListing.dequeueReusableCell(withIdentifier: cellIdentifier) as! MenuPopupTBCell;
if (cell == nil) {
cell = MenuPopupTBCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier);
}
cell.btnCategoryTitle.imageView?.contentMode = .scaleAspectFit
let data = arrCategoryData[indexPath.row] as! [String:Any]
cell.btnCategoryTitle.setTitle(data["title"] as? String, for: .normal)
let id = data["id"] as! Int
if arrSelectedRows.contains(id){
cell.btnCategoryTitle.setImage(UIImage(named:"checkBoxChecked"), for: .normal)
}else{
cell.btnCategoryTitle.setImage(UIImage(named:"checkBocUnchecked"), for: .normal)
}
cell.btnCategoryTitle.tag = id
cell.btnCategoryTitle.addTarget(self, action: #selector(checkBoxSelection(_:)), for: .touchUpInside)
return cell;
}
@objc func checkBoxSelection(_ sender:UIButton)
{
if self.arrSelectedRows.contains(sender.tag){
self.arrSelectedRows.remove(at: self.arrSelectedRows.index(of: sender.tag)!)
}else{
self.arrSelectedRows.append(sender.tag)
}
self.tblMenuListing.reloadData()
}
This way you get all the selected id in the
arrSelectedRows
so you can use it as required
Output:-
回答3:
you can use a collection to determine if an item is checked or not. and add the following:
var items: [(text: String, checked: Bool)] = [] //example, can be what ever you want
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell() //you can reuse cell for performance
let item = items[indexPath.row]
cell.textLabel?.text = item.text //set text
cell.accessoryType = item.checked ? .checkmark : .none //mark cell base on array
return cell
}
and in the other methods (didSelect & didDeselect) update the data in the array
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
items[indexPath.row].checked = true //add this
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .none
items[indexPath.row].checked = false //add this
}
}
after this modifications - your table view won't mark wrong cell again
来源:https://stackoverflow.com/questions/50112804/add-remove-multiple-checkmarks-on-selected-rows-in-swift-4-uitableview