打开TableView和Select Row

时间:2017-09-04 12:19:01

标签: ios swift uitableview ios10-today-widget

我的应用程序的今天小部件有一个UITableView。每当用户点击一行时,我想打开iOS主应用程序,导航到UIViewController的{​​{1}},然后选择相应的单元格。

我知道打开应用程序,我会做这样的事情:

UITableView

并选择行,我会这样做:

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

我如何从Today Widget一起完成这一切呢?

1 个答案:

答案 0 :(得分:1)

将您的索引转换为字符串,并使用自定义网址方案附加

ProjectName://indexPath.row as string

最终基于索引

项目名:// 1 项目名:// 2

然后使用附加值

重定向到您的主应用
let url = URL(string: "ProjectName://1")
extensionContext?.open(url!, completionHandler: nil)

它将调用您的主应用程序应用程序委托,您可以通过转换回 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
        }

viewcontroller 中观察并将其转换为 Int

viewDidLoad

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

然后添加此方法

func navigateToPushnotificationScreen(notification : NSNotification){

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