删除NSTimer / UITableViewCell实时?

我有一个UITableView ,我在每个UITableViewCell上设置了许多定时器。 每个计时器从用户在我的应用程序上创build“发布”开始,并应在24小时内过期。 但是,我想这样,当所有24小时结束时, UITableViewCell实时删除自己,但我似乎无法弄清楚我应该删除计时器的时间或地点。 我有一个方法,将不断刷新计时器每秒使用NSTimer.scheduledTimerWithTimeInterval ,它NSTimer.scheduledTimerWithTimeInterval更新每个UITableViewCell上的计时器。 但是,我无法find一个方法或find如何find每个UITableViewCell中的每个计时器是否完成。 很明显,我可以find如果计时器是在viewDidLoad完成,但只有在视图变为活动状态时调用。 有没有我缺less的方法或任何我可以用来查找是否通过scheduledTimerWithTimeInterval方法计时器完成,如果是,删除它? 这里是我的代码如下:

 //I have a self.offers array declared in the beginning of my class whcih will act as the UITableView data source. var offers = [Offer]() func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { //Dequeue a "reusable" cell let cell = tableView.dequeueReusableCellWithIdentifier(offerCellIdentifier) as! OfferCell setCellContents(cell, indexPath: indexPath) return cell } func setCellContents(cell:OfferCell, indexPath: NSIndexPath!){ let item = self.offers[indexPath.row] cell.offerName.text = item.offerName() cell.offerPoster.text = item.offerPoster() var expirDate: NSTimeInterval = item.dateExpired()!.doubleValue //Get current time and subtract it by the predicted expiration date of the cell. Subtract them to get the countdown timer. var timeUntilEnd = expirDate - NSDate().timeIntervalSince1970 if timeUntilEnd <= 0 { //Here is where I want to delete the countdown timer but it gets difficult to do so when you are also inserting UITableViewCells and deleting them at the same time. self.offers.removeAtIndex(indexPath!.row) self.offersReference = Firebase(url:"<Database Link>") self.offersReference.removeValue() self.tableView.reloadData() cell.timeLeft.text = "Finished." } else{ //Display the time left var seconds = timeUntilEnd % 60 var minutes = (timeUntilEnd / 60) % 60 var hours = timeUntilEnd / 3600 cell.timeLeft.text = NSString(format: "%dh %dm %ds", Int(hours), Int(minutes), Int(seconds)) as String } } override func viewDidLoad() { super.viewDidLoad() var timeExpired = false //I set up my offers array above with completion handler setupOffers { (result, offer) -> Void in if(result == true){ //Insert each row one by one. var currentCount = self.offers.count var indexPaths: [NSIndexPath] = [NSIndexPath]() indexPaths.append(NSIndexPath(forRow:currentCount, inSection: 0)) self.offers.append(offer) currentCount++ self.tableView.reloadData() } } // Do any additional setup after loading the view, typically from a nib. self.tableView.delegate = self self.tableView.dataSource = self self.tableView.rowHeight = UITableViewAutomaticDimension self.tableView.rowHeight = 145.0 } //Called when you click on the tab override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) self.refreshTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "refreshView:", userInfo: nil, repeats: true) //Should fire while scrolling, so we need to add the timer manually: //var currentRunLoop = NSRunLoop() //currentRunLoop.addTimer(refreshTimer, forMode: NSRunLoopCommonModes) } override func viewDidDisappear(animated: Bool) { super.viewDidDisappear(animated) refreshTimer.invalidate() refreshTimer = nil } //Constantly refreshes the data in the offers array so that the time will continuously be updating every second on the screen. func refreshView(timer: NSTimer){ self.tableView.reloadData() } 

你有一些误解,你的代码有一些问题,你的描述不清楚。

如果使用scheduledTimerWithTimeInterval创build一个计时器,则不需要,也不应该将其添加到runloop。 scheduledTimerWithTimeInterval方法为你做。

如果您创build一个重复计时器,它永远不会“完成”。 它不断重复。 如果您创build一个不重复的计时器,它会触发一次,然后消失。 你不需要删除它。 只是不要强烈地提到它,它一旦被触发就会被释放。

你说你为每个表视图单元格创build一个计时器,但是你发布的代码只会为视图控制器创build一个计时器。 你说:“…它每秒更新每个UITableViewCell上的计时器,但是我找不到一个方法,或者find每个UITableViewCell里面的每个计时器是否完成。 这与您发布的代码不符。 你的代码每秒运行一次计时器,只是告诉表视图重新加载数据。

我想你的意思是当你的定时器触发时,你在每个单元格中重新显示剩余的时间计数器?

那么,您是否在问如何计算所有单元格的item.timeLeft()剩余时间是否已达到零? 你还没有发布代码或timeLeft()函数的要求,所以你的读者不能告诉应该发生什么。