2016-11-24 1 views
1

저는 초급자로 지금부터 몇 달 동안 신속하게 학습을 시작합니다. 타이머 무효화 문제로 도와주세요. 나를 어리 석게 느끼게 자유롭게해라. :). 좀 지저분한 것 같아요. 다음 번에 더 잘 할 수있는 방법을 알려주고 있습니다.신속한 타이머 무효화, tableView 셀 내

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

스위프트를 사용하는 경우 3 NSIndexPath가 IndexPath로 변경되었습니다. IndexPath (행 : 행, 섹션 : 0)를 시도하십시오. –