UITableView Checkmarks disappear when scrolling
Asked Answered
M

5

4

I have to make checkmarks on a tableView, but if I'm scrolling and one check marked cell is not visible and I scroll back the checkmark disappeared.

While running this code

var boolArray = [Bool]()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)      {




        var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!


        if cell.accessoryType == UITableViewCellAccessoryType.Checkmark {

            cell.accessoryType = UITableViewCellAccessoryType.None

            boolArray[indexPath.row] = false


        }
        else
        {

            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

            boolArray[indexPath.row] = true

        }

    println(boolArray)


}
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell
{
    boolArray.append(false)
        var view = UITableViewCell(style: UITableViewCellStyle.Default,    reuseIdentifier: "CellTable")


        return view

}

After a little bit scrolling and checkmarking, the printed array is this big...

[true, false, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]

Materiel answered 13/1, 2015 at 9:19 Comment(0)
F
4
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell{
var cell : UITableViewCell = .........
if(boolArray[indexPath.row){
    cell.accessoryType = UITableViewCellAccessoryType.Checkmark
} else {
    cell.accessoryType = UITableViewCellAccessoryType.None
}
}

Try this code.

Footer answered 13/1, 2015 at 9:36 Comment(1)
You forget a ] after indexPath.rowAdeleadelheid
C
4

Create an Array of tuples to store row and section values:

var selectedCells:[(row: Int, section: Int)] = []

In your cellForRowAtIndexPath, check if you have any selectedCellValues. If so, add an accessoryType for that cell and break out of the loop,

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCellWithIdentifier("SettingsCell", forIndexPath: indexPath) as SettingsCustomCell

    var rowNums:NSNumber = indexPath.row
    var sectionNums:NSNumber = indexPath.section

    var selectedRow:Int
    var selectedSection:Int

    if(selectedCells.count > 0){
        for tuple in selectedCells{

            selectedRow = tuple.row
            selectedSection = tuple.section

            if(selectedRow == rowNums && selectedSection == sectionNums){
                cell.accessoryType = UITableViewCellAccessoryType.Checkmark
                break
            }else{
                cell.accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }else{
        cell.accessoryType = UITableViewCellAccessoryType.None
    }

    var keyValue = listOfSectionTitles[indexPath.section]
    var items: [NSString] = dictionaryOfCellTitles.objectForKey(keyValue) as [NSString]

    cell.textLabel?.text = items[indexPath.row]
    return cell
}

Handle selectedcellvalues in didSelecteRowAtIndexPath:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let cell = tableView.cellForRowAtIndexPath(indexPath)

    if(cell?.accessoryType == UITableViewCellAccessoryType.None){
        cell?.accessoryType = UITableViewCellAccessoryType.Checkmark
        selectedCells.append(row:indexPath.row, section:indexPath.section)
    }else{
        var rowToDelete = indexPath.row
        var sectionToDelete = indexPath.section

        for var i=0; i < selectedCells.count; i++
        {
            if(rowToDelete == selectedCells[i].row && sectionToDelete == selectedCells[i].section){
                selectedCells.removeAtIndex(i)
                cell?.accessoryType = UITableViewCellAccessoryType.None
            }
        }
    }
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Campus answered 13/3, 2015 at 20:1 Comment(0)
M
1

An NSMutableIndexSet is a better object to use to track selection status.

You also need to check the selection status when you return the cell for an indexPath

var selectedCells = NSMutableIndexSet()

func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)      {

    var accessory=UITableViewCellAccessoryType.none

    if (selectedCells.containsIndex(indexPath.row) {
       selectedCells.removeIndex(indexPath.row)
    }
    else {
       selectedCells.addIndex(indexPath.row)
       accessory=.checkmark
    }

    if let cell = tableView.cellForRowAtIndexPath(indexPath) {

        cell.accessoryType = accessory
    }

}

func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) ->    UITableViewCell
{
    var cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier:"CellTable")

     var accessory=UITableViewCellAccessoryType.none

     if (selectedCells.containsIndex(indexPath.row) {
         accessory = .checkmark
     }

     view.accessoryType=accessory

        return view
}
Mistreat answered 13/1, 2015 at 9:55 Comment(0)
S
0

Your error is in cellForRowAtIndexPath when you do boolArray.append(false). cellForrRowAtIndexPath: is called every time a cell will be drawn even if it has been drawn before. A possible fix is only appending false if indexPath.row is greater than the length of boolArray, otherwise it is your 'model' and you just check it.

Schrecklichkeit answered 13/1, 2015 at 9:30 Comment(0)
P
0
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell

reuses cells as you can see from the code. You need to keep the state of your selected cells, and assign the accessory views state after var view = UITableViewCell ...

Preposition answered 13/1, 2015 at 9:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.