[英]segue not called immediately
I have a view controller A and it has a table list created dynamically with cells when calling didselectcell as 我有一个视图控制器A,当调用didselectcell时,它具有一个用单元动态创建的表列表
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if(cell?.tag == 1){
performSegueWithIdentifier("profile", sender: self)
print("perform segue for favourite")
}
and i am calling prepare for segue like this and saving the variable the variable is available in 2nd view controller 我在打电话给segue这样准备并保存变量,该变量在第二个视图控制器中可用
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue == "profile"){
let vc = segue.destinationViewController as! MembersViewController
vc.title = " "
}
when i run the app it takes hardly more than 25 sec [ the second view controller has collectionview] any idea of doing it faster 当我运行该应用程序时,几乎不需要超过25秒的时间[第二个视图控制器具有collectionview]可以更快地完成它的任何想法
I solved this by calling dispatch_async function it moves immediately to next page and the next page also has for loop in viewwillappear thanks @rakeshbs got answer from this link 我通过调用dispatch_async函数解决了此问题,该函数立即移至下一页,并且下一页在viewwillappear中也会出现for循环,谢谢@rakeshbs从此链接获得了答案
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if(cell?.tag == 1){
dispatch_async(dispatch_get_main_queue(), {
self.performSegueWithIdentifier("profile", sender:self)
})
}
There are a few things wrong with this. 这有一些问题。 Change your didSelectRow
method to this. 将您的didSelectRow
方法更改为此。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if(indexPath.row == 0) {
performSegueWithIdentifier("profile", sender: nil)
}
}
You can fix your didSelectRowAtIndexPath
method to remove needing to grab the cell. 您可以修复didSelectRowAtIndexPath
方法,以消除需要获取单元格的didSelectRowAtIndexPath
。 It has a reference to the cell's index already, which is what you were grabbing. 它已经引用了单元格的索引,这正是您要获取的。
Also, your prepareForSegue
method can be improved as follows: 另外,您的prepareForSegue
方法可以改进如下:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "profile") {
let vc = segue.destinationViewController as? MembersViewController
vc?.title = "New Title"
}
}
You should never need to call dispatch_async
with something like this, especially in Swift, unless you're already on a Background thread. 除非您已经在使用后台线程,否则您永远不需要使用类似这样的命令来调用dispatch_async
,尤其是在Swift中。
Before using the UITableView
object and its UITableViewDelegate
and UITableViewDataSource
components, I suggest you muck about in an Xcode Playground, and read the Apple Documentation carefully to understand how everything works. 在使用UITableView
对象及其UITableViewDelegate
和UITableViewDataSource
组件之前,建议您在Xcode Playground中仔细研究一下,并仔细阅读Apple文档以了解一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.