2014-10-08 42 views
3

我前面贴 - 如何阻止多个细胞能够在我的表视图中选择 - 答案是使用tableView.AllowsMultipleSelections=false的TableView AllowsMultipleSelection - 仍然允许多重选择

我一直在努力,自惨遭失败,基本上它似乎并不服从这一规则。

我的期望输出应该一个小区选择和有一个绿色的勾旁边(这工作),如果选择一个新的小区,老勾去掉,并移动到新的当前单元格。

我已经尝试了许多方法和最新的是从选择的最后一个索引创建自己的索引路径 - 然后将配件以零和更改选定为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; 
    } 

} 

enter image description here

enter image description here

在提出的解决方案尝试

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

你设置的tableview委托自我? – ytbryan 2014-10-08 15:47:59

+0

我拖着表视图,视图控制器黄色按钮,在顶部 - 将图像添加到原来的职位现在 – Biscuit128 2014-10-08 15:49:32

回答

4

不要修改单元格didSelectRowAtIndexPath。将国家设置为“已选中”,将前一个设为“未选中”,然后重新加载表格数据。当调用cellForRowAtIndexPath时,使用选定(或不)状态更新单元格。

+0

嗨 - 感谢你的回复。我已经加了我在原来的职位底部的尝试,我仍然未能惨败:( – Biscuit128 2014-10-08 16:07:22

+2

你还在试图改变在'didSelectRowAtIndexPath'细胞内。我想说的是,备份数据需要知道它是否。被选择,而不必在'self.countries'一个字符串,你有某种“国家”的对象,它知道它的名字和它是否被选中 – 2014-10-08 16:43:00

+0

啊确定这是有道理的 - 现在我会尝试这一点 - 谢谢 – Biscuit128 2014-10-08 16:44:35

相关问题