2015-12-07 27 views
0

如何通过单击选择和取消选择此UITableView中的所有单元格?如何使用一个UIButton从UITableView中选择和取消选择所有复选标记?

这个按钮有问题。所以,我的想法是,当我点击它时,UITableView中的所有单元格都有一个复选标记(select),当我再次单击它时,所有单元格都没有复选标记(取消选择)。

我已经实现了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 
    } 

PLS,让我知道。 谢谢!

+0

您需要首先让您的数据模型跟踪当前选择哪些行。 – rmaddy

+1

您不能跟踪单元格中的选择状态(使用附件类型),您需要其他一些变量。我推荐一个NSMutableIndexSet。选择一个单元格意味着您将该行添加到集合中,取消选择意味着将其删除。选择全部通过addIndexesInRange完成,取消全部选择只是为了清除索引集 – Paulw11

回答

3

想象你的UITableViewCells只是显示你的数据在你的dataSource中,它更容易认为他们只能呈现信息并呈现新的信息,你必须更新你的数据源并重新加载tableview。

我看到你的数据源是这是一个字符串数组。如果将项目的类型更改为String和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)] 

正如你所看到的阵列中的每个元素上面包含字符串和布尔,这个布尔代表着一个国家是否被选中与否。因此,要“检查”所有单元格,我们将更改数据源中的值并重新加载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从,而不是直接改变细胞得到它的数据的数据源。

0

正如其他人指出的那样,您必须为您的位置和选择状态维护一个数据模型,以便轻松处理您的场景。您可以为您的位置信息定义一个简单的模型。在您的ViewController类中定义此模型struct

选址模型

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 
    } 
} 
相关问题