如何在viewController中取消选中我选中的复选标记

时间:2015-12-05 07:16:59

标签: ios swift2 viewcontroller checkmark

导入UIKit

class RightSideViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate {

var tableView: UITableView!
var textField: UITextField!
var menuItems:[String]=["A","B","C","All"];

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

//处理可以重新创建的任何资源。     }

@available(iOS 2.0, *)
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return menuItems.count;
}

@available(iOS 2.0, *)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let leftCell = tableView.dequeueReusableCellWithIdentifier("RightCell", forIndexPath: indexPath) as! RightSideTableViewCell
    leftCell.projectsName.text = self.menuItems[indexPath.row]


    return leftCell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let mySelectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!


    // when find checkmark then unchecked

    if mySelectedCell.accessoryType == .Checkmark
    {
        mySelectedCell.accessoryType = UITableViewCellAccessoryType.None
    }
    else
    {
        mySelectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
        print(menuItems[indexPath.row])

    }

   // when click on "all" deselect all selected checks 
    if indexPath.row == menuItems.count-1
    {
       // i want to deselect my all selected checkmark here
    }




}

}

我无法取消选择点击支票

1 个答案:

答案 0 :(得分:0)

您需要将所选单元格的状态存储在NSMutableSet中,然后您需要引用此集合以从所选单元格中删除复选标记。 (就像保罗正确地说的那样)。您可以参考以下答案。

Parse and Swift. Get cells in UITableView which have a checkmark. "Cannot invoke argument.“