简体   繁体   中英

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
}

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
}

Tableview单元格具有附件类型,在tableviewCellForRowAtIndexPath中使用此类型

cell.accessoryType = UITableViewCellAccessoryCheckmark;

you show check mark as below .

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM