[英]Open TableView and Select Row
我的應用有今天的小部件,其中有UITableView
。 每當用戶點擊一行時,我都想打開主iOS應用程序,使用UITableView
導航到UIViewController
,然后選擇對應的單元格。
我知道要打開該應用程序,我會執行以下操作:
let url = URL(string: "ProjectName://")
extensionContext?.open(url!, completionHandler: nil)
並選擇行,我會這樣做:
tableView.selectRowAtIndexPath(index, animated: true, scrollPosition: UITableViewScrollPosition.middle)
但是,如何從“今日”小部件中一起完成所有這些工作?
將您的索引轉換為字符串並附加自定義url方案
ProjectName://indexPath.row作為字符串
最終基於索引
ProjectName:// 1 ProjectName:// 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.