TableView AllowsMultipleSelection-仍允许多个选择

Bis*_*128 4 uitableview ios swift

我之前发布过-关于如何阻止在表格视图中选择多个单元格-答案是使用 tableView.AllowsMultipleSelections=false

从那以后,我一直在尝试并遭受惨痛的失败,从本质上讲,它似乎并没有遵守该规则。

我希望的输出应该是选中一个单元格,并在其旁边有一个绿色的勾号(这有效),如果选择了一个新的单元格,则旧的勾号将被删除并移至新的当前单元格。

我尝试了许多方法,最新的方法是从最后选择的索引创建我自己的索引路径-然后将附件视图设置为nil并将所选视图更改为false。这也不起作用。请有人帮我解决我可能做错的事情。

谢谢

import UIKit

class QuizViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    var countries = ["Germany", "France", "England", "Poland", "Spain"];

    var selected = -1;

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.allowsMultipleSelection = false;

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



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

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        println("indexpath \(indexPath.row)");
        let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = self.countries[indexPath.row];
        cell.textLabel?.textAlignment = NSTextAlignment.Center;


        let countryImage = String(self.countries[indexPath.row]) + ".png";
        cell.imageView?.image = UIImage(named: countryImage);

        let image: UIImageView = UIImageView();
        cell.accessoryView = image;
        return cell;
    }


    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {

        var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell

        cell.textLabel?.text = self.countries[indexPath.row];
        cell.textLabel?.textAlignment = NSTextAlignment.Center;


        let countryImage = String(self.countries[indexPath.row]) + ".png";
        cell.imageView?.image = UIImage(named: countryImage);


        let imageName = "tick.png";
        let image: UIImageView = UIImageView(image: UIImage(named: imageName));
        cell.accessoryView = image;

        if(selected != -1){
            //lets remove the tick from the previously selected cell and set selected to false

            var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: NSIndexPath(forRow: selected, inSection: 0)) as UITableViewCell

            cell.accessoryView = nil;
            cell.selected = false;
        }

        selected = indexPath.row;
    }

}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

尝试提出建议的解决方案

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = self.countries[indexPath.row];
    cell.textLabel?.textAlignment = NSTextAlignment.Center;

    let countryImage = String(self.countries[indexPath.row]) + ".png";
    cell.imageView?.image = UIImage(named: countryImage);


    if(cell.selected){
        println("cell was set to selected at \(indexPath.row)");
        let imageName = "tick.png";
        let image: UIImageView = UIImageView(image: UIImage(named: imageName));
        cell.accessoryView = image;
    }else{
        println("cell was NOT set to selected at \(indexPath.row)");
        let image: UIImageView = UIImageView();
        cell.accessoryView = image;
    }

    return cell;
}


func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {


    var cell = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: indexPath) as UITableViewCell
    cell.selected = true;

    if(selected != -1){
        var previous = self.tableView.dequeueReusableCellWithIdentifier("CountryCell", forIndexPath: NSIndexPath(forRow: selected, inSection: 0)) as UITableViewCell
        previous.selected = false;
    }
    selected = indexPath.row;
    tableView.reloadData();
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*lls 5

请勿修改中的单元格didSelectRowAtIndexPath。将国家/地区设置为“选定”,将上一个国家/地区设置为“未选定”,然后重新加载表格数据。使用选定(或不选择)状态在cellForRowAtIndexPath调用时可视地更新单元格。

  • 您仍在尝试更改`didSelectRowAtIndexPath`中的单元格。我的意思是支持数据需要知道是否已选择。因此,您将拥有某种“ Country”对象,该对象知道其名称以及是否被选中,而不是在“ self.countries”中包含字符串。 (2认同)