简体   繁体   中英

How to update a UITableViewCell in firstVC when a change occurs in secondVC automatically and in realtime

I have a editviewcontroller(secondVC)(which you access by tapping on the uitableviewcell task) where you get the option of setting a reminder. When you set the reminder; an icon appears in front of the task in the UITableViewCell in the firstVC.Now I want that once the reminder is triggered and a notification is sent the icon from the task is removed in realtime. Currently, the way I have set it; if you visit the editVC after the task has been reminded, i compare the current time to time set by the user and then updated a label which says "Time's up".

I want a similar thing to occur with the appropriate cell in the firstVC.

FIRSTVC : FIRSTVC

When the time is up, it tells you that time is up and when you return to the firstVC the bell icon is removed. But I want it to happen in realtime even if you are in the firstVC and you don't have to go to the secondVC and then return to firstVC to get the changes.

In short, i want the bell icon to be removed when a task has been reminded to the user which is set in the secondVC. Thanks!

EditVC: 在此处输入图片说明

Code: The following code is executed in the editVC in viewDidLoad. If the current time is more than the time selected, it changes the label to "Time's Up" and changes the bellicon tintcolor to white for that specific reminder.

      guard let selectedDate = editnotes?.sSelectedDate,
        var needsToRemind = editnotes?.sReminderState else {

            print("nil")
            return
    }

    if selectedDate <= Date() && needsToRemind {
        editnotes?.sReminderDate = "Time's up"
        editnotes?.belliconcolor = .white
        reminderMsg.text = editnotes?.sReminderDate

    }

您可以用来触发发布通知。发布通知用于执行操作,而无需转到特定的VC。

Do you know about post notification or Notification center? If yes, then it is easy to implement in your code otherwise you need some R&D on its. First of all, register post notification on first vc then after on secondvc fire this notification which is register on first vc. It's simple. If you can't get it then i will send some code for easily got it.

You can fire when your timer stop. And one more important things that when you fire notification , you must need to pass current stop time. Because this time is used to first vc method which is register. In this method, you can compare your reminder time and your current time which is passed by notification if both are same then you can hide the bell otherwise not. One more thing, please Manage array to accurate the code after then reload table.

One problem with your code that I can see right away is that you will only say "Time's up" in your edit VC's viewDidLoad . What happens if time is up a few seconds (or even a second) after viewDidLoad ?

If this were my code, I would have a Timer property on BOTH the editVC and on the parent (or first) view controller. It would look like this:

var timesUpTimer : Timer?

Which you can set up in viewWillAppear :

if let selectedDate = editnotes?.sSelectedDate
{
    self.timesUpTimer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(doSomething), userInfo: nil, repeats: false)
    RunLoop.main.add(timesUpTimer, forMode: RunLoopMode.commonModes)
}

// in the editVC you can have this:
func doSomething()
{
    editnotes?.sReminderDate = "Time's up"
    editnotes?.belliconcolor = .white
    reminderMsg.text = editnotes?.sReminderDate
}

and you would do the appropriate thing in the doSomething you have in the firstVC.

Also, in viewWillDisappear for each view controller, you need to invalidate and set your Timer property to nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM