Karthickhari Karthickhari - 16 days ago 6
iOS Question

How to show Checkmark in tableview swift?

I have to show check mark before i selected for some array values, i have to show the checkmark for array value 1 and none for 0.How to do that..check the code below:

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

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
cell.textLabel?.text = AllItems[indexPath.row] as? String
for dict1 in selectedItems
{
if Intvalues == dict1 as? NSObject {
// I have to show CheckMark
}
else if ZeroIntvalues == dict1 as? NSObject
{
// I don’t need to show CheckMark



}
}
cell.textLabel?.textColor = UIColor.whiteColor()
return cell
}

Answer

Because of reason @Paulw11 said, your code is wrong. Each tableViewCell is displayed checkmark because the last object of for loop is always 1.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
    cell.textLabel?.text = AllItems[indexPath.row] as? String
        if Intvalues == AllItems[indexPath.row] as? Int  {
           // I have to show CheckMark
          cell.accessoryType = .Checkmark
         } else {
          cell.accessoryType = .None
         }
    cell.textLabel?.textColor = UIColor.whiteColor()
    return cell
}