1

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
                   
                }
            }    
        }
abarisone
  • 3,707
  • 11
  • 35
  • 54
globalworld
  • 33
  • 1
  • 8

3 Answers3

2

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..

Pavan Gandhi
  • 1,729
  • 1
  • 22
  • 36
  • thanks but in tableviewdidselect method when i write cell.accessoeyType it showing me error i.e unresolve identifier is used ,also i, add object and remove object method but it showing that array has no such memer – globalworld May 05 '16 at 05:04
  • Sorry i was used wrong logic. I send u code snaps so you can just followed given code and i run and check this its work fine. – Pavan Gandhi May 05 '16 at 05:26
  • i had question arise when i have to select only one item by checkmark how can i do that and previous checkmark get remove so please help me – globalworld May 09 '16 at 07:22
  • So you can do with them in diddeselect method – Pavan Gandhi May 09 '16 at 08:17
  • you have to checked that array count > 1 then u have to remove all values from array and simply add that selected cells unique value as i did previously – Pavan Gandhi May 09 '16 at 08:18
  • 1
    now simple logic that array contains only one everytime so in cellforrow its shows only selected indexpath value as checkmark – Pavan Gandhi May 09 '16 at 08:20
  • thanks in between two values i have to select only one if i select first value then second get automatically unmark – globalworld May 09 '16 at 08:25
1

For Swift 3 following worked for me to

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    yourtableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .checkmark
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    yourtableView.cellForRow(at: indexPath as IndexPath)?.accessoryType = .none
}
0
cell.textLabel?.text = self.dataArray[indexPath.row] as? String

This suggests that dataArray contains strings.

if dataArray.containsObject(indexPath)

This suggests that dataArray contains index paths. Both of these should not be true. One array for data makes sense, and another one for selected rows or rows to be checked also makes sense, but not the same array for both.

What's probably happening is:

  • Row selected - cell is then updated to have the checkmark accessory
  • Table scrolled and cellForRowAtIndexPath is called - at no point will dataArray contain an index path, so the accessory is always cleared.

You need to be updating your model when a row is selected, to store the index path of the selected row, or update a selected flag on your model objects.

jrturton
  • 118,105
  • 32
  • 252
  • 268