如何使用附件复选标记

时间:2016-01-15 10:38:47

标签: ios swift uitableview

我有两个部分

1.MapViewController

2.TypesTableViewController

当我运行我的应用程序并调用TypesTableViewController时,它打开时显示所有选中的单元格我希望它不被选中 请帮助我并检查我的代码

1.MapViewController

class MapViewController: UIViewController {

    @IBOutlet weak var mapCenterPinImage: UIImageView!
    @IBOutlet weak var pinImageVerticalConstraint: NSLayoutConstraint!
    var searchedTypes = ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Types Segue" {
            let navigationController = segue.destinationViewController as! UINavigationController
            let controller = navigationController.topViewController as! TypesTableViewController
            controller.selectedTypes = searchedTypes
            controller.delegate = self
        }
    }
}

// MARK: - TypesTableViewControllerDelegate
extension MapViewController: TypesTableViewControllerDelegate {

    func typesController(controller: TypesTableViewController, didSelectTypes types: [String]) {
        searchedTypes = controller.selectedTypes.sort()
        dismissViewControllerAnimated(true, completion: nil)
    }

}

2.TypesTableViewController

protocol TypesTableViewControllerDelegate: class {
    func typesController(controller: TypesTableViewController, didSelectTypes types: [String])
}

class TypesTableViewController: UITableViewController {


    let possibleTypesDictionary = ["bakery":"Bakery", "bar":"Bar", "cafe":"Cafe", "grocery_or_supermarket":"Supermarket", "restaurant":"Restaurant"]
    var selectedTypes: [String]!
    weak var delegate: TypesTableViewControllerDelegate!
    var sortedKeys: [String] {
        return possibleTypesDictionary.keys.sort()
    }

    // MARK: - Actions
    @IBAction func donePressed(sender: AnyObject) {
        delegate?.typesController(self, didSelectTypes: selectedTypes)
    }

    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return possibleTypesDictionary.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TypeCell", forIndexPath: indexPath) 
        let key = sortedKeys[indexPath.row]
        let type = possibleTypesDictionary[key]!
        cell.textLabel?.text = type
        cell.imageView?.image = UIImage(named: key)
        cell.accessoryType = (selectedTypes!).contains(key) ? .Checkmark : .None
        return cell
    }

    // MARK: - Table view delegate
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let key = sortedKeys[indexPath.row]
        if (selectedTypes!).contains(key) {
            selectedTypes = selectedTypes.filter({$0 != key})
        } else {
            selectedTypes.append(key)
        }

        tableView.reloadData()
    }
}

2 个答案:

答案 0 :(得分:1)

不确定您在代码中做了什么。如果要取消选中,请将以下行更改为

cell.accessoryType = (selectedTypes!).contains(key) ? .Checkmark : .None

cell.accessoryType = (selectedTypes!).contains(key) ? . None : . Checkmark

更新: - 仅获取复选标记单元格的答案的第二部分,

更改如下

@IBAction func donePressed(sender: AnyObject) {
   let rowCount = tableView.numberOfRowsInSection(0)
   selectedTypes.removeAll()
  for var index = 0; index < rowCount; ++index {
      let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as! YourCell
      if cell.accessoryType = .Checkmark{
         let key = sortedKeys[index]
         selectedTypes.append(key)
      }
    delegate?.typesController(self, didSelectTypes: selectedTypes)
  }
}

答案 1 :(得分:0)

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    //toggle checkmark on and off
    if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }
    else {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    
    //add animation so cell does not stay selected
    tableView.deselectRow(at: indexPath, animated: true)
    
}