2016-11-24 16 views
1

我是一個初學者,剛開始幾個月就開始快速學習。請幫我解決計時器失效問題。隨意讓我感到很蠢:)。我知道這有點混亂,我願意指示下一次如何更好地做到這一點。快捷計時器在桌子內部無效查看單元格

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCustomerCell 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 

    let object = appDelegate.Queuelist[indexPath.row] 

    if let genderInital = object.gender?.characters.first { 
     cell.genderlabel.text = " \(genderInital)" 
    } 
    object.timerStarted() 


    cell.waitedTimeLabel.text = "\(object.counter)" 

    cell.nameLabel.text = object.name 
    cell.languageLabel.text = object.language 
    cell.reasonLabel.text = object.reasonOfVisit 
    return cell 
} 

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

    var customerName: String? 


    if let indexPathForName = tableView.indexPathForSelectedRow { 

     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     let selectedCustomer = appDelegate.Queuelist[indexPathForName.row] 
     customerName = selectedCustomer.name 
     } 


    let alertController = UIAlertController(title: customerName, message: "message", preferredStyle: .alert) 

    let cancelAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil) 

    let editAction = UIAlertAction(title: "edit", style: .default) {action -> Void in 

     self.performSegue(withIdentifier: "addSegue", sender: alertController) 
     } 
    let takeCustomerAction = UIAlertAction(title: "take customer", style: .default) { 
     action -> Void in 
     if let indexpath = tableView.indexPathForSelectedRow { 
      let appDelegate = UIApplication.shared.delegate as! AppDelegate 

      let object = appDelegate.Queuelist[indexpath.row] 
      object.timer?.invalidate() 
      appDelegate.Queuelist.remove(at: indexpath.row) 
      appDelegate.storedQueuelist() 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
    } 

    alertController.addAction(takeCustomerAction) 
    alertController.addAction(editAction) 
    alertController.addAction(cancelAction) 
    self.present(alertController, animated: true, completion: nil) 
} 
+2

你能解釋一下你越試圖完成的任務,你遇到了什麼問題,你嘗試過什麼辦法呢? – Jelle

回答

0

如果您爲每個單元分配計時器。你只需要在CustomCustomerCell類中聲明方法startTimer()和endTimer()。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{ 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCustomerCell 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let object = appDelegate.Queuelist[indexPath.row] 

    if let genderInital = object.gender?.characters.first { 
     cell.genderlabel.text = " \(genderInital)" 
    } 
    cell.startTimer() 

    cell.waitedTimeLabel.text = "\(object.counter)" 
    cell.nameLabel.text = object.name 
    cell.languageLabel.text = object.language 
    cell.reasonLabel.text = object.reasonOfVisit 

    return cell 
    } 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
//Use below method if you want to end timer for all cells 
     //endTimerForAllCells(tableView) 
    var customerName: String? 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let selectedCustomer = appDelegate.Queuelist[indexPath.row] 
    customerName = selectedCustomer.name 

    let alertController = UIAlertController(title: customerName, message: "message", preferredStyle: .alert) 

    let cancelAction = UIAlertAction(title: "cancel", style: .cancel, handler: nil) 

    let editAction = UIAlertAction(title: "edit", style: .default) {action -> Void in 
    self.performSegue(withIdentifier: "addSegue", sender: alertController) 
    } 

    let takeCustomerAction = UIAlertAction(title: "take customer", style: .default) { 
    action -> Void in 

    if let indexpath = tableView.indexPathForSelectedRow { 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 

     if let cell = tableView.cellForRowAtIndexPath(indexPath) as? CustomCustomerCell{ 
     cell.endTimer() 
     } 
     appDelegate.Queuelist.remove(at: indexpath.row) 
     appDelegate.storedQueuelist() 
     tableView.deleteRows(at: [indexPath], with: .fade) 
     } 
    } 

    alertController.addAction(takeCustomerAction) 
    alertController.addAction(editAction) 
    alertController.addAction(cancelAction) 
    self.present(alertController, animated: true, completion: nil) 
    } 

//Below method is use to end timer of all cells 
func endTimerForAllCells(tableView:UITableView){ 
    let appDelegate = UIApplication.shared.delegate as! AppDelegate 
    let totalRows = appDelegate.Queuelist 
    if totalRows.count != 0{ 
     for row in 0...totalRows.count - 1{ 
      let index = NSIndexPath(forRow: row, inSection: 0) 
      if let cell = tableView.cellForRowAtIndexPath(index) as? CustomCustomerCell{ 
       cell.endTimer() 
       } 
      } 
      } 
     } 
     } 

CustomCustomerCell類中定時器的聲明方法。

private var timer: Timer? 
func startTimer(){ 

    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(methodToDoSomethingAtEachInterval), userInfo: nil, repeats: true) 
} 

func endTimer() { 
    timer?.invalidate 
    timer = nil 
} 

func methodToDoSomethingAtEachInterval(){ 
//Do something at each time interval for each tableviewCell 
} 
+0

這是一個模糊問題的答案,但是從代碼發佈來看@JeffreyChang正在尋找啓動/停止對象而不是UITableViewCell' – jjatie

+0

我得到這行代碼的錯誤 let index = NSIndexPath(forRow:row,inSection:0) 標籤錯誤與任何可用的重載不匹配。那是什麼意思? –

+0

如果您使用swift 3 'NSIndexPath'已更改爲'IndexPath' try IndexPath(row:row,section:0) –