2015-12-07 28 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 
    } 
} 
相關問題