Open TableView and Select Row

313 views Asked by At

I have a today widget for my app which has a UITableView. Whenever a user taps on a row, I want to open up the main iOS app, navigate to the UIViewController with the UITableView., and then select the coresponding cell.

I know to open the app, I would do something like this:

let url = URL(string: "ProjectName://")
extensionContext?.open(url!, completionHandler: nil)

and to select the row, I would do this:

tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.middle)

How would I do this all together from the Today Widget, though?

1

There are 1 answers

0
karthikeyan On

Conver your index as string and append with your custom url scheme

ProjectName://indexPath.row as string

finally based on index

ProjectName://1 ProjectName://2

Then redirect to your main app using with append values

let url = URL(string: "ProjectName://1")
extensionContext?.open(url!, completionHandler: nil)

It will call your main app application delegate, there you can get your index by converting back to Int

     func application(_ application: UIApplication, open urls: URL, sourceApplication: String?, annotation: Any) -> Bool {

                let obj = urls.absoluteString.components(separatedBy: "://")[1]

//Here convert string to object back to int
                NotificationCenter.default.post(name:"selectrow", object: obj)


            return true
        }

In your viewcontroller observe it and convert it to Int

In viewDidLoad

 NotificationCenter.default.addObserver(self, selector: #selector(ViewController.navigateToPushnotificationScreen), name: "selectrow", object: nil)

Then add this method

func navigateToPushnotificationScreen(notification : NSNotification){

       let  index = notification.object as! String
       //Here convert your string object to Int, and select your tableview
    }