swiftuitableviewcheckboxcellcheckmark

how to add checkmark in tableview swift


I'm trying to show checkmark on tableview cell, but the checkmark appears only sometimes and it disappears when I scroll.

Below the code:

 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCellWithIdentifier("vvxxx12", forIndexPath: indexPath)
    
        // Configure the cell...
        
    cell.textLabel?.text = self.dataArray[indexPath.row] as? String //in dataArray values are stored
             

       if dataArray.containsObject(indexPath)
       {
            cell.accessoryType = .Checkmark
       }
       else {
            cell.accessoryType = .None
        }
       return cell
         }

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
            if let cell = tableView.cellForRowAtIndexPath(indexPath) {
                if cell.accessoryType == .Checkmark {
                    cell.accessoryType = .None
                   
                } else {
                    cell.accessoryType = .Checkmark
                   
                }
            }    
        }


Solution

  • Just do following changes in your code to maintain checkmark into tableview while you are scrolling tableview

    enter image description here

    Result :

    enter image description here

    Its work fine now, Any problem let me know i will definitely help you to out.Enjoy..