2

I have a table view and one of the table view cells opens another app. When I return to my app the table view cell is still highlighted. What is the best way to deselect a table view cell when returning to the app?

Edit: The issue is that -viewWillAppear or -viewDidAppear does not get called when returning from an app since the view is already visible.

Pang
  • 9,564
  • 146
  • 81
  • 122
Berry Blue
  • 15,330
  • 18
  • 62
  • 113

2 Answers2

1

Set notification in viewDidLoad

final override func viewDidLoad() {
    super.viewDidLoad()

    // add notification observers
    NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

Create method didBecomeActive

func didBecomeActive() {
    if let indexPath = tableView.indexPathForSelectedRow {
        deselectRow(at: indexPath, animated: true)
    }
}

UIKit documentation

Adrian Bobrowski
  • 2,681
  • 1
  • 16
  • 26
  • @Sneak chill out. It think my answer was accepted because it presented example code. Answer about notification i have from other ask: http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun – Adrian Bobrowski Feb 26 '17 at 22:19
  • Just because I point out some moral values to a free community where people use their valuable time to answer to problems for free doesnt mean I am not chill. Just a heads up for you, next time you copy a solution from an answer 1 hour later, make sure you atleast add a note giving the credit to the original solution that was posted 1 hour before yours. Nothing wrong with editing your answer to a **completely new one** based on another answer, however, make sure you credit the author, or the link you took the solution of if you say so. –  Feb 26 '17 at 22:24
  • Our answers are completely different. The only similarity is `UIApplicationDidBecomeActive` – Adrian Bobrowski Feb 26 '17 at 22:27
  • @AdrianBobrowsku And Who pointed out to you that viewDidAppear does not get called? http://oi64.tinypic.com/2rmazpc.jpg That you had to change your answer 1 hour later? I don't see any comments here, oh yeah, thats right, my comment in my answer and my answer to this :) Im out, enjoy your "rep farming". –  Feb 26 '17 at 22:29
  • @Sneak What does this prove? And now you delete your answer. Why? – Adrian Bobrowski Feb 26 '17 at 22:44
  • Dude, stop chatting now, I told you already, update your answer and give credit to the original author http://stackoverflow.com/questions/5277940/why-does-viewwillappear-not-get-called-when-an-app-comes-back-from-the-backgroun where you said you got the answer from. Thats all that I am asking. Let's stop the chat now. Also, I noted a couple downvotes on random answers of mine directly after your last comment and have contacted the moderators to look into it. –  Feb 26 '17 at 22:55
1

You have 2 options for solve your issue

Option 1

Deselect row in didSelectRow method Example code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // Open app code here
}

Option 2

Find selected row and deselect row,

Put below code in viewDidAppear method

for (NSIndexPath *indexPath in tableView.indexPathsForSelectedRows) {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
Adrian Bobrowski
  • 2,681
  • 1
  • 16
  • 26
Vivek
  • 4,916
  • 35
  • 40