Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Checkmarks disappear when scrolling

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]

like image 742
Michael Krautsieder Avatar asked Jan 10 '23 02:01

Michael Krautsieder


1 Answers

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.

like image 157
Nhut Duong Avatar answered Jan 18 '23 16:01

Nhut Duong