gpt4 book ai didi

ios - TableView AllowsMultipleSelection - 仍然允许多项选择

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:41 30 4
gpt4 key购买 nike

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

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

我想要的输出应该是一个单元格被选中并且旁边有一个绿色勾号(这有效)如果一个新的单元格被选中,旧的勾号被删除并移动到新的当前单元格。

我尝试了很多方法,最新的方法是从最后选择的索引创建我自己的索引路径——然后将附件 View 设置为 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;
}

}

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

最佳答案

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

关于ios - TableView AllowsMultipleSelection - 仍然允许多项选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26261015/

30 4 0
文章推荐: javascript - 带有背景图片的自定义形状
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com