TableView AllowMultipleSelection - Still Allows Multiple Selection

I posted earlier - about how to stop selection of multiple cells in my table view - the answer was to use tableView.AllowsMultipleSelections=false

I try and fail because, in fact, it doesn't obey this rule.

My desired result is to be selected with one cell and there is a green check mark next to it (this works), if a new cell is selected, the old tick is removed and moved to the new current cell.

I've tried many approaches and the last one is to create a custom index path from the last selected index - then set the accessory to zero and change the value to false. It doesn't work either. Please help me with what I might be doing wrong.

thank

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;
    }

}

      

enter image description here

enter image description here

Trying the proposed solution

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

      

+3


source to share


1 answer


Do not change cell in didSelectRowAtIndexPath

. Set the country as "selected" and the previous one as "not selected" and then reload the table data. Use the selected (or not) state to visually update the cell on invocation cellForRowAtIndexPath

.



+4


source







All Articles