2017-03-21 154 views
1

我有一個UITableView與UITableViewCell,我想通過單擊它來選擇它們並通過另一次單擊它們來取消選擇它們。 首先,我與如何通過點擊選擇並取消選擇一行

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { 
     // code 
} 

嘗試過,但我注意到,當你點擊一個細胞第二次這個功能沒有得到執行;當你點擊另一個單元格時它會被執行。 但我希望你可以點擊幾個單元格,當你點擊它們時被選中。

所以我做了這個代碼(簡化):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print("Selected") 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LektionTableViewCell 
     if cell.accessoryType == .none { 
      print("Type = none") 
      cell.accessoryType = .checkmark 
      Lektion.insert(Dateien[indexPath.row], at: Lektion.count) 
     } else if cell.accessoryType == .checkmark { 
      print("Type = check") 
      cell.accessoryType = .none 
      Lektion.remove(at: indexPath.row) 
     } 
    } 

但它不能正常工作:

  1. 當我點擊一個單元格,文本自動轉到「標籤」 (它在視圖生成器中的文字)
  2. 當我點擊一個帶有支票的單元時,支票不會消失,Xcode會說「Type = none」;那是錯的。

有人能幫助我嗎?

+0

該代碼無法工作,**永遠不會**調用'cellForRow'之外的'dequeueReusableCell'。將'Bool'屬性'selected'添加到您的模型中,將其設置爲'didSelectRow'並重新加載該行。在'cellForRow'中,根據'selected'設置accessoryType。 – vadian

+0

@vadian我試過了,但我不知道如何編碼。你能在答案中對我說嗎? – Blub

+0

'Dateien'的類型是什麼?順便說一句,變量名稱應該以小寫字母開頭。請在該問題中添加「cellForRow」。 – vadian

回答

0

didSelect方法中刪除以下代碼,因爲它不應該在cellForRowAtIndexPath之外使用dequeReusableCell

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LektionTableViewCell 
     if cell.accessoryType == .none { 
      print("Type = none") 
      cell.accessoryType = .checkmark 
      Lektion.insert(Dateien[indexPath.row], at: Lektion.count) 
     } else if cell.accessoryType == .checkmark { 
      print("Type = check") 
      cell.accessoryType = .none 
      Lektion.remove(at: indexPath.row) 
     } 

你可以進去使用cellForRowAtIndexPath細胞例如idSelectRow

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    if let cell = tableView.cellForRow(at: IndexPath) as? LektionTableViewCell { 
     // Perform your operations here..... 
    } 

} 

現在,解決您的查詢,對於didSelect沒有在點擊調用(除非您選擇其他行)。 允許您選擇多個表格視圖

注意:在您的表格數組中加入一個標誌(boolean)變量來設置選擇狀態。

+0

但是爲什麼在'let cell = tableView.cellForRow(at:IndexPath)'之前如果? LektionTableViewCell {'? – Blub

+0

在「let cell = ....」之前使用「if」不是強制的。您可以直接使用>>「let cell = tableView.cellForRow(at:IndexPath)as!LektionTableViewCell」這是防止異常的預防措施... I一般用於與多個表格和單元格互相整合... – Krunal

+0

我有過相同的想法,只有'let cell = tableView.cellForRow(at:IndexPath)' – Blub

0

我不能評論,所以我在這裏添加它。 從您的代碼中,我們看不到您存儲數據以填充單元格的位置。所以我不知道這是否能幫助你,但假設你有一個數組與對象,一種方法是在你的對象上設置一個「isSelected」變量,方法是didSelectRowAt indexPath,然後調用tableview.reloadData()。事情是這樣的:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      myObjects[indexPath.row].isSelected = !myObjects[indexPath.row].isSelected 
      tableView.reloadData() 
     } 

然後在cellForRowAtIndexPath方法,當你創建你的細胞,你可以很容易地設置accessoryType取決於你的對象的標誌或者.checkmark或.none。 想要這樣:

cell.accessoryType = myObjects[indexPath.row].isSelected ? .checkMark : .none 

希望有幫助。

相關問題