我有一個UITableView
其中我有無數的定時器設置在每個UITableViewCell
。每個計時器從用戶在我的應用程序上創建「發佈」開始,並應在24小時內過期。但是,我希望它在所有24小時結束時,UITableViewCell
實時刪除自己,但我似乎無法弄清楚我應該刪除計時器的位置或時間。我有一個方法,將不斷刷新計時器每秒使用NSTimer.scheduledTimerWithTimeInterval
,它每秒鐘更新每個UITableViewCell
上的計時器。但是,我無法找到一個方法或找到如何找到每個UITableViewCell中的每個計時器是否完成。顯然,我可以找到定時器是否在viewDidLoad
中完成,但只有在視圖變爲活動狀態時才調用該定時器。有沒有我缺少的方法或任何我可以用來查找通過scheduledTimerWithTimeInterval
方法的計時器是否完成,如果是,刪除它?這裏是我的代碼如下:實時刪除NSTimer/UITableViewCell?
//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()
}
嗨,謝謝你讓我知道runloop。我會相應地解決這個問題。關於重複計時器,我希望重複它,因爲我希望它實時更新計時器。如果我將'scheduledTimerWithTimeInterval'中的'repeat:'參數設置爲'false',則定時器不會實時更新。我對這個問題的最後部分表示歉意,因爲我不想發佈太多的代碼。我會相應地編輯它,並試圖使事情更清楚。 – user1871869
我上面發佈了我的修改。我已經拿出了一些誤導性的代碼,並展示了我的'UITableViewCells'是如何工作的。我以前爲此道歉。所以基本上我想要做的就是'timeLeft()'變爲0時,我想實時從'UITableView'中移除這個單元。但是,我意識到在setCellContents中執行此操作並不是實現此目的的最佳方法,因爲在插入到我的'tableView'中時也調用此方法,因此我發生衝突。我想知道是否有一種方法可以在每個單元的計時器達到0時找到,然後完全刪除該單元。 – user1871869
沒關係,我修復了一切。上面的代碼工作。謝謝你的幫助! – user1871869