ios - 如何使用一个 UIButton 选择和取消选择 UITableView 中的所有复选标记?

标签 ios swift uitableview uibutton selectall

如何一键选择和取消选择此 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
    }

请告诉我。 谢谢!

最佳答案

将您的 UITableViewCells 看作是简单地显示您的数据源中的数据,更容易认为它们只能呈现信息并且呈现新信息,您必须更新数据源并重新加载 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)]

正如你在上面看到的,数组中的每个元素都包含一个字符串和一个 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 从中获取数据的数据源,而不是直接更改单元格。

关于ios - 如何使用一个 UIButton 选择和取消选择 UITableView 中的所有复选标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34142906/

相关文章:

Swift:如何在 Core Data 中进行过滤

ios - Swift collectionView 选择单元格多选

ios - Firebase 获取用户数据 - Swift

ios - UITableView 的 LikeButton

ios - 标签栏中的表格 View 未重新加载

ios - 选择一个指定的 subview swift

ios - 在UIAccessibilityContainer中,使用 `accessibilityElements`和其他三种方法有什么区别?

ios - UITableView 无法正确重新加载

iOS - 根据 TableView 选定的行自定义 segue ViewController 的最简单方法是什么?

iphone - 为什么我的 XCode 单元测试根本不起作用?