UITableViewCell checkmark to be toggled on and off when tapped

前端 未结 14 1436
执念已碎
执念已碎 2020-11-30 22:25

I\'m working on a tableview

I want to be able to tap on each cell and when tapped, it displays a checkmark on the cell

Now I have some code that makes this w

相关标签:
14条回答
  • 2020-11-30 22:52

    The simplest solution that did it for me (Swift 5.2)

    override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
        // Remove checkmark from the row that is currently showing it before adding to one being selected
        if let currentIndexPath = tableView.indexPathForSelectedRow {
            self.tableView.cellForRow(at: currentIndexPath)?.accessoryType = .none
        }
    
        return indexPath
    }
    
    override public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    
    0 讨论(0)
  • 2020-11-30 22:55

    Swift 3.0
    Using just one function to keep it simple

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            if cell.accessoryType == .checkmark {
                cell.accessoryType = .none
            } else {
                cell.accessoryType = .checkmark
            }
        }   
    }
    
    0 讨论(0)
  • 2020-11-30 22:56

    A UITableView keeps selected state for single or multiple selections. So IMO there would need to be a very good reason for keeping an entire parallel state somewhere. If you want to just change the cell's appearance based on select state, do it in the cell.

    In your UITableViewCell subclass, override setSelected like so:

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.accessoryType = selected ? .checkmark : .none
    }
    

    No need to use any table view delegate methods.

    Note: You have to call super.setSelected otherwise the cell doesn't keep the selected state correctly.

    0 讨论(0)
  • 2020-11-30 22:56

    Swift 4.0, all together now:

    import UIKit
    
    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var yourData = ["Cool","Sweet","Awesome"]
    
    var checked = [Bool]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        checked = Array(repeating: false, count: yourData.count)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return searchData.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt IndexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    
        //configure you cell here.
        if checked[IndexPath.row] == false{
            cell.accessoryType = .none
        } else if checked[IndexPath.row] {
            cell.accessoryType = .checkmark
        }
    
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    
        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            if cell.accessoryType == .checkmark {
                cell.accessoryType = .none
                checked[indexPath.row] = false
            } else {
                cell.accessoryType = .checkmark
                checked[indexPath.row] = true
            }
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-30 22:57
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        if self.checkedIndex == indexPath.row{
    
        }else{
            let cell = tableView.cellForRow(at: indexPath)
            cell?.accessoryType = .checkmark
            let indexPathh = IndexPath(row: checkedIndex, section: 0)
            let UnCheckCell = tableView.cellForRow(at: indexPathh)
            UnCheckCell?.accessoryType = .none
            checkedIndex = indexPath.row
        }
    }
    
    0 讨论(0)
  • 2020-11-30 22:59

    Try this:

    var checked = [Bool]() // Have an array equal to the number of cells in your table
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
    
        //configure you cell here.
        if !checked[indexPath.row] {
            cell.accessoryType = .None
        } else if checked[indexPath.row] {
            cell.accessoryType = .Checkmark
        }
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if cell.accessoryType == .Checkmark {
                 cell.accessoryType = .None
                 checked[indexPath.row] = false
            } else {
                 cell.accessoryType = .Checkmark
                 checked[indexPath.row] = true
            }
        }    
    }
    

    To reset all the checkboxes:

    func resetChecks() {
       for i in 0.. < tableView.numberOfSections {
           for j in 0.. < tableView.numberOfRowsInSection(i) {
                if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: j, inSection: i)) {
                   cell.accessoryType = .None
                }
           }
       }
    }
    
    0 讨论(0)
提交回复
热议问题