我之前发布过 - 关于如何阻止在我的表格视图中选择多个单元格 - 答案是使用tableView.AllowsMultipleSelections=false
我一直在努力和悲惨地失败,因为基本上它似乎并不遵守这条规则。
我想要的输出应该是一个单元格被选中并且旁边有一个绿色标记(这个工作)如果选择了一个新单元格,旧标记将被删除并移动到新的当前单元格。
我尝试了很多方法,最新的方法是从最后选择的索引创建自己的索引路径 - 然后将附件视图设置为nil并将selected更改为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;
}
}
尝试提出解决方案
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 :(得分:5)
不要修改didSelectRowAtIndexPath
中的单元格。将国家/地区设置为"已选择"而前一个是"未选中"然后重新加载表数据。在调用cellForRowAtIndexPath
时,使用选定(或不)状态可视地更新单元格。