簡體   English   中英

取消選中TableView在Swift4中的所有TableView選擇

[英]Uncheck TableView all TableView Selections in Swift4

我已經搜索過,還沒有找到解決方案:

  • 我有一個tableView,允許多個選擇
  • 我想有一個按鈕,可以在Swift中同時清除所有選擇。 該按鈕將位於與TableView不同的viewController( UIViewController )上。 是否可以使用協議和委托,還是需要單身人士? 我希望它在單擊按鈕時更新。

幾年前,我在這里看到了Dejan Skledar的回答: 點擊時可以打開和關閉UITableViewCell復選標記,而名為resetChecks()的函數似乎是我想要的,但是我很難讓Swift編寫的代碼起作用。 當我運行它時,它實際上並沒有進入該方法,然后在調用該方法時,它崩潰並顯示“意外找到nil”。 請,有人可以提供一些建議嗎? 謝謝。 這是我寫的:

func removeChecks() {
    for i in 0...grainTableView.numberOfSections - 1 {
        for j in 0...grainTableView.numberOfRows(inSection: i) - 1 {
            if let cell = tableView(grainTableView, cellForRowAt: [i,j]) as? UITableViewCell {
                cell.accessoryType == .none 
            }
        }
    }
}

當您展示currentVC時,從preVC發送對象

class currentVC:UIViewController {

    var delegate:PreVC?

 func removeChecks() {

   if let grainTableView = delegate?.tableView {
      for i in 0...grainTableView.numberOfSections - 1 {
          for j in 0...grainTableView.numberOfRows(inSection: i) - 1 {
              if let cell = tableView(grainTableView, cellForRowAt: [i,j]) as? UITableViewCell {
                cell.accessoryType == .none 
             }
          }
      }
   }
}

//

func removeChecks() {

   if let grainTableView = delegate?.tableView {

      delegate?.shouldClear = true

      grainTableView.reloadData()

   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM