user7142042
user7142042

Reputation:

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
}

Upvotes: 1

Views: 2947

Answers (4)

nynohu
nynohu

Reputation: 1668

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
}

Upvotes: 3

Gourav Joshi
Gourav Joshi

Reputation: 2419

Here is an approach that I have used in my app,

If you are using webservice, then after calling a webservice implement this logic:

this code in webservice calling section:

for i in 0..<arrNL.count {
   let dict = arrNL[i].mutableCopy() as! NSMutableDictionary
   dict.setObject("0", forKey: "isChecked")
   self.arrNotificationList.addObject(dict)
}
// Here 0 indicates that initially all are uncheck.

Now manage condition in CellForRowAtIndexPath:

let dict = arrNotificationList[indexPath.row] as! NSDictionary
if(dict["isChecked"] as! String == "0") { // Unchecked
   cell.btn_CheckUnCheck.setImage(UIImage(named: "uncheck"), forState: UIControlState.Normal)
} else { // Checked
   cell.btn_CheckUnCheck.setImage(UIImage(named: "check"), forState: UIControlState.Normal)
}
// in this condition you can manage check uncheck status    

Now code for a button from which we can check or uncheck it

let dict = arrNotificationList[indexValue] as! NSMutableDictionary
if(dict["isChecked"] as! String == "1") {
   dict.setObject("0", forKey: "isChecked")
} else {
   dict.setObject("1", forKey: "isChecked")
}
arrNotificationList.replaceObjectAtIndex(indexValue, withObject: dict)
tblView.reloadData()

Upvotes: 1

KKRocks
KKRocks

Reputation: 8322

you show check mark as below .

  cell.accessoryType = .None
if Intvalues == dict1 as? NSObject  {
     // I have to show CheckMark
     cell.accessoryType = .Checkmark
}

Upvotes: 1

mukul
mukul

Reputation: 382

Tableview cell has accessory type , Use this in tableviewCellForRowAtIndexPath

cell.accessoryType = UITableViewCellAccessoryCheckmark;

Upvotes: 1

Related Questions