在我的應用程序中,我正在研究類似於正常鬧鐘應用程序如何讓您重複幾天的事情。在重複日子的UITableViewController中,一週中的每一天(週日,週一等)有7個單元格。我想檢查哪些單元格被勾選,然後將文本標籤(在另一個視圖控制器中)更改爲被勾選的單元格(文本:週日,週一,週二,如果星期日,星期一和星期二單元格被勾選)。檢查單元格中是否有複選標記
I嘗試使用for循環並遍歷每個單元格,然後創建if語句,但是如果需要在此處使用語句,則有太多方法。如果你們中的任何一位能夠向我展示實現這一目標的更簡單方法,我將非常感激。以下是我的代碼供參考。
class RepeatVC: UITableViewController {
var repeatText: String? = nil //Text to change in this vc and set in other vc
// var checked: Bool? = false
@IBOutlet var repeatView: UITableView! //UITableView in TableViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showRepeat" { //Segue identifier for segue to this viewcontroller
let displayAddEditAlarm = segue.destination as! AddEditAlarm
displayAddEditAlarm.repeatLabel.text = repeatText //Text I am trying to change in the other viewcontroller
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if(tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark){
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
// // checked = false
// print("\(checked!)")
}
else{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
// // checked = true
// print("\(checked!)")
}
// if(checked == true){
// if(tableView.cellForRow(at: indexPath)?.tag == 0){
// repeatText = "Sunday!"
// print("\(repeatText!) is the repeated day!!!")
// }
// }
}
這裏有一些照片,解釋我想要什麼:
您選擇您需要取消時設置'isSelected'屬性,用於細胞TRUE;或'FALSE'細胞後,我相信 – Lamar
@Lamar請你寫,你覺得會適合我的代碼的答案。我覺得我會從中學到更多東西,我會很感激一個解釋。謝謝! –
您需要保留,跟蹤另一個數據結構中選定的單元格;我建議'設置'或'設置'。然後,您可以簡單地使用該集的內容來確定選擇哪些天。不要使用單元本身來跟蹤狀態,因爲單元格被重用並且僅僅是數據的視圖。 –
Paulw11