我有一個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)
}
}
但它不能正常工作:
- 當我點擊一個單元格,文本自動轉到「標籤」 (它在視圖生成器中的文字)
- 當我點擊一個帶有支票的單元時,支票不會消失,Xcode會說「Type = none」;那是錯的。
有人能幫助我嗎?
該代碼無法工作,**永遠不會**調用'cellForRow'之外的'dequeueReusableCell'。將'Bool'屬性'selected'添加到您的模型中,將其設置爲'didSelectRow'並重新加載該行。在'cellForRow'中,根據'selected'設置accessoryType。 – vadian
@vadian我試過了,但我不知道如何編碼。你能在答案中對我說嗎? – Blub
'Dateien'的類型是什麼?順便說一句,變量名稱應該以小寫字母開頭。請在該問題中添加「cellForRow」。 – vadian