2017-07-14 43 views
-1

在我的應用程序中,我正在研究類似於正常鬧鐘應用程序如何讓您重複幾天的事情。在重複日子的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!!!") 
//   } 
//  } 

    } 

這裏有一些照片,解釋我想要什麼:

Checkmark Cells Label Text

+0

您選擇您需要取消時設置'isSelected'屬性,用於細胞TRUE;或'FALSE'細胞後,我相信 – Lamar

+0

@Lamar請你寫,你覺得會適合我的代碼的答案。我覺得我會從中學到更多東西,我會很感激一個解釋。謝謝! –

+0

您需要保留,跟蹤另一個數據結構中選定的單元格;我建議'設置'或'設置'。然後,您可以簡單地使用該集的內容來確定選擇哪些天。不要使用單元本身來跟蹤狀態,因爲單元格被重用並且僅僅是數據的視圖。 – Paulw11

回答

0

我通過執行以下步驟解決了我的問題。 我第一次把swift文件連接到我的uitableviewcontroller。 然後,我創建了一個名爲weekdays的變量,並將它變爲一種布爾值數組。所有7個值都被設置爲false,這意味着它們沒有一個在開始時被檢查。然後,在didSelectRowAt中,我將它設置爲如果勾選標記存在且選擇了相同單元格(意味着它將更改爲無附件類型),則與標記對應的單元格將設置爲false值。如果沒有附件類型,我反之亦然(這意味着如果選擇它將被檢查)。然後,我在準備繼續,說,如果這些值是真的,然後顯示像週日,週末,每天,或單獨的日子。

import Foundation 
import UIKit 

class RepeatVC: UITableViewController { 



    var lastSelection: NSIndexPath! 
    var repeatText = "" 

    var checked: Bool? = false 

    var weekdays: [Bool] = [false, false, false, false, false, false, false] 


    override func viewDidLoad() { 
     super.viewDidLoad() 



     self.navigationItem.title = "Repeat" 
    } 


    @IBOutlet var repeatView: UITableView! 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "backFromRepeat" { 

      if(weekdays[0] == true){ 
       repeatText += "Sun " 
      } 

      if(weekdays[1] == true){ 
       repeatText += "Mon " 
      } 

      if(weekdays[2] == true){ 
       repeatText += "Tue " 
      } 

      if(weekdays[3] == true){ 
       repeatText += "Wed " 
      } 

      if(weekdays[4] == true){ 
       repeatText += "Thu " 
      } 

      if(weekdays[5] == true){ 
       repeatText += "Fri " 
      } 

      if(weekdays[6] == true){ 
       repeatText += "Sat " 
      } 

      if(weekdays[0] == true && weekdays[1] == true && weekdays[2] == true && weekdays[3] == true 
       && weekdays[4] == true && weekdays[5] == true && weekdays[6] == true){ 
       repeatText = "Daily" 
      } 

      else if(weekdays[0] == true && weekdays[6] == true){ 
       repeatText = "Weekends" 
      } 

      else if(weekdays[1] == true && weekdays[2] == true && weekdays[3] == true 
       && weekdays[4] == true && weekdays[5] == true){ 
       repeatText = "Weekdays" 
      } 
      else if(weekdays[0] == false && weekdays[1] == false && weekdays[2] == false && weekdays[3] == false 
       && weekdays[4] == false && weekdays[5] == false && weekdays[6] == false){ 
       repeatText = "Never" 
      } 

      let displayAddEditAlarm = segue.destination as! AddEditAlarm 
      displayAddEditAlarm.repeatLabel.text = repeatText 
      print("\(repeatText) is my text") 
     } 
    } 


    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 
      if(tableView.cellForRow(at: indexPath)?.tag == 0){ 
       weekdays[0] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 1){ 
       weekdays[1] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 2){ 
       weekdays[2] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 3){ 
       weekdays[3] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 4){ 
       weekdays[4] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 5){ 
       weekdays[5] = false 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 6){ 
       weekdays[6] = false 
      } 
     } 
     else{ 
      tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark 

      if(tableView.cellForRow(at: indexPath)?.tag == 0){ 
       weekdays[0] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 1){ 
       weekdays[1] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 2){ 
       weekdays[2] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 3){ 
       weekdays[3] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 4){ 
       weekdays[4] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 5){ 
       weekdays[5] = true 
      } 
      if(tableView.cellForRow(at: indexPath)?.tag == 6){ 
       weekdays[6] = true 
      } 
      print(weekdays) 

     } 


    } 
相關問題