实时删除NSTimer / UITableViewCell? [英] Delete NSTimer/UITableViewCell in real time?

查看:76
本文介绍了实时删除NSTimer / UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView ,其中我在每个 UITableViewCell 上设置了多个计时器。每个计时器从用户在我的应用程序上创建帖子时开始,并且应该在24小时内到期。但是,我想要它,以便当所有24小时结束时, UITableViewCell 实时删除自己,但我似乎无法弄清楚我应该删除的地点或时间计时器。我有一个方法,将使用 NSTimer.scheduledTimerWithTimeInterval 每秒不断刷新计时器,并更新每个 UITableViewCell 上的计时器每一秒。但是,如果每个UITableViewCell中的每个计时器都完成,我找不到方法或找到我怎么找到的方法。显然我可以找到计时器是否在 viewDidLoad 中完成,但只有在视图变为活动状态时才会调用。有没有我缺少的方法或任何我可以用来查找是否通过 scheduledTimerWithTimeInterval 方法完成的计时器,如果是,要删除它?这是我的代码如下:

I have a UITableView in which I have numerous timers set on each UITableViewCell. Each timer starts from when a user creates a "post" on my application and should expire within 24 hours. However, I want it so that when all 24 hours is over, the UITableViewCell deletes itself in real time but I can't seem to figure out where or when I should be deleting the timer. I have a method that will constantly refresh the timer every second using NSTimer.scheduledTimerWithTimeInterval and it updates the timers on each UITableViewCell every second. However, I can't find a method or find how I can find if each timer inside each UITableViewCell is finished. Obviously I can find if the timer is finished in viewDidLoad but that is only called right when the view becomes active. Is there any method I am missing or anything I can use to find if a timer via the scheduledTimerWithTimeInterval method is finished, and if it is, to delete it? Here is my code below:

//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()
}


推荐答案

您有许多误解,代码存在一些问题,而且说明不明确。

You have a number of misconceptions, a few problems with your code, and your description isn't clear.

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

If you create a timer using scheduledTimerWithTimeInterval, you don't need to, and shouldn't, add it to the runloop. The scheduledTimerWithTimeInterval method does that for you.

如果你创建一个重复计时器,它永远不会完成。它不断重复。相反,如果您创建一个非重复计时器,它会触发一次然后消失。您无需删除它。只是不要强烈引用它,一旦它触发它就会被解除分配。

If you create a repeating timer, it never "finishes." It keeps repeating forever. If instead you create a non-repeating timer, it fires once and then goes away. You don't need to delete it. Just don't keep a strong reference to it and it will be deallocated once it fires.

你说你为每个表视图单元创建一个计时器,但代码你posted仅为视图控制器创建单个计时器。你说...它每秒更新每个UITableViewCell上的计时器。但是,如果每个UITableViewCell中的每个计时器都完成,我找不到方法或找到我怎样找到的方法。这与您发布的代码不符。你的代码每秒运行一次计时器,它只是告诉表视图重新加载它的数据。

You say you create a timer for each table view cell, but the code you posted only creates a single timer for the view controller. You say "...it updates the timers on each UITableViewCell every second. However, I can't find a method or find how I can find if each timer inside each UITableViewCell is finished." That doesn't match the code you posted. Your code is running a timer once a second that simply tells the table view to reload it's data.

我想你的意思是你重新显示每个计时器的剩余时间计数器计时器启动时的单元格?

I guess you mean that you re-display a remaining time counter in each cell when your timer fires?

那么,您是否在询问如何判断所有单元格的剩余时间 item.timeLeft()已达到零?您尚未发布 timeLeft()函数的代码或要求,因此您的读者无法确定应该发生的事情。

So, are you asking how to figure out if the time remaining for all of your cell's item.timeLeft() has reached zero? You haven't posted the code or the requirements for your timeLeft() function, so your readers can't tell what is supposed to be happening.

这篇关于实时删除NSTimer / UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆