如何只需点击一下即可从此UITableView中选择和取消选择所有单元格?
我遇到这个按钮的问题。因此,我的想法是,当我点击它时,UITableView中的所有单元格都有一个复选标记(选择),当我再次单击它时,所有单元格都没有复选标记(取消选择)。
我已经实现了UIButton在点击后更改名称(从“全选”到“取消全选”,反之亦然)。
这些是我写的:
let locationItems:[String] = ["China", "United States", "South Africa", "Spain", "Australia", "Brazil", "Colombia", "Nigeria", "India"]
var selectedItemsIndex:Int?
var selectIndicator = true
@IBOutlet var tableViewWithOptions: UITableView!
@IBOutlet var selectAllLabel: UIButton!
@IBAction func selectAllButton(sender: AnyObject) {
if (selectIndicator == true) {
if selectAllLabel.titleLabel?.text == "Select all" {
selectAllLabel.setTitle("Deselect all", forState: UIControlState.Normal)
}
selectIndicator = false
} else {
if selectAllLabel.titleLabel?.text == "Deselect all" {
selectAllLabel.setTitle("Select all", forState: UIControlState.Normal)
}
selectIndicator = true
}
tableViewWithOptions.reloadData()
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let index = selectedItemsIndex {
let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0))
cell?.accessoryType = .None
}
let cell = tableView.cellForRowAtIndexPath(indexPath)
cell?.accessoryType = .Checkmark
}
请告诉我。 谢谢!
答案 0 :(得分:2)
将您的UITableViewCells视为您在dataSource中的数据的简单显示,您可以更容易地认为他们只能提供信息并提供更新数据源和重新加载tableview的新信息。
我看到你的数据源是locationItems
,这是一个字符串数组。如果将项目类型更改为String和Bool类型的元组,则可以将位置名称和bool分配给数组中的单个元素,bool表示已选中(true)或未选中(false)
所以将locationItems
更改为:
//out data source, contains an array of Tuples containing a string and a bool
let locationItems:[(String, Bool)] = [("China", true), ("United States",false), ("South Africa", false), ("Spain", true), ("Australia", true), ("Brazil", false), ("Colombia", true), ("Nigeria", true), ("India", true)]
如上所示,数组中的每个元素都包含一个String和一个Bool,这个bool表示是否检查了一个国家。
所以要检查'在您的所有单元格中,我们将更改数据源中的值并重新加载tableView。这将导致再次调用cellForRow
,然后单元格将从我们更新的数据源中获取新数据。
@IBAction func selectAllButton(sender: AnyObject) {
//loop through our data source and change the bool in each element to true
//to indicate that all locations are checked
for item in locationItems {
//access second item in the tuple which is our bool and set it's value to true
item.1 = true
}
tableView.reloadData()
}
因此,您执行此操作的一般要点是编辑tableView从中获取数据的数据源,而不是直接更改单元格。
答案 1 :(得分:0)
正如其他人所指出的那样,您必须为您的位置及其选择状态维护数据模型,以便轻松处理您的方案。您可以为您的位置信息定义一个简单的模型。在struct
课程中定义此模型ViewController
。
位置模型
struct Location {
var name: String
var selected: Bool
init(_ name: String, _ selected:Bool) {
self.name = name
self.selected = selected
}
}
现在您的模型已定义,您需要加载数据源和属性的初始值。 [注意:您还可以使用帮助程序类来填充数据源。]
var selectionStateIndicator = false
var locationItems: [Location] = {
var _locations = [Location]()
var locationNames = ["China", "United States", "South Africa", "Spain", "Australia", "Brazil", "Colombia", "Nigeria", "India"]
for location in locationNames {
_locations.append(Location(location, false))
}
return _locations
}()
更新cellForRowAtIndexPath
方法以使用新数据模型显示数据。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = self.locationItems[indexPath.row].name
cell.accessoryType = self.locationItems[indexPath.row].selected ? .Checkmark : .None
return cell
}
更新selectAllButton
操作以使用新模型对象并更新每行的选择状态。
@IBAction func selectAllButton(sender: AnyObject) {
selectionStateIndicator = !selectionStateIndicator
for var location in locationItems {
location.selected = selectionStateIndicator
}
tableViewWithOptions.reloadData()
}
更新didSelectRowAtIndexPath
方法,以便每当有单独的行选择时,相应地更新行。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
locationItems[indexPath.row].selected = !locationItems[indexPath.row].selected
if !locationItems[indexPath.row].selected {
cell?.accessoryType = .None
} else {
cell?.accessoryType = .Checkmark
}
}