如何通过单击选择和取消选择此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,让我知道。 谢谢!
您需要首先让您的数据模型跟踪当前选择哪些行。 – rmaddy
您不能跟踪单元格中的选择状态(使用附件类型),您需要其他一些变量。我推荐一个NSMutableIndexSet。选择一个单元格意味着您将该行添加到集合中,取消选择意味着将其删除。选择全部通过addIndexesInRange完成,取消全部选择只是为了清除索引集 – Paulw11