检查一个swift 2之前无法取消检查所有单元格

fan*_*dro 0 uitableview checkmark swift

我使用swift 2和UITableViews,当我按下一个单元格时会出现一个复选标记,但我不想在我的tableview中只检查一个单元格,所以其他复选标记将从我的tableview中消失.我尝试了不同的技术而没有成功.我有一个只有标签的CustomCell.

这是我的代码:

import UIKit


class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    @IBOutlet weak var tableView: UITableView!

    var answersList: [String] = ["One","Two","Three","Four","Five"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return answersList.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomCell
        cell.displayAnswers(answersList[indexPath.row]) // My cell is just a label       
        return cell
    }

    // Mark: Table View Delegate

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // Element selected in one of the array list
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        if let cell = tableView.cellForRowAtIndexPath(indexPath) {
            if cell.accessoryType == .Checkmark {
                cell.accessoryType = .None
            } else {
                cell.accessoryType = .Checkmark
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 5

假设你只有这里的部分是你可以做的

// checkmarks when tapped

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let section = indexPath.section
    let numberOfRows = tableView.numberOfRowsInSection(section)
    for row in 0..<numberOfRows {
        if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) {
            cell.accessoryType = row == indexPath.row ? .Checkmark : .None
        }
    }
}
Run Code Online (Sandbox Code Playgroud)