2016-10-17 70 views
2

我有一些UITableView从内部array数据。我想显示UIAlertController,但我遇到了非常奇怪的延迟。奇怪的延迟与警报在UITableView

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    print("tapped \(dispatcher.conversations[indexPath.row].name)") //this one works fine, no problems here 

    let message = dispatcher.conversations[indexPath.row].desc + "\n\nDo you wanna play this?" 
    let alertname = dispatcher.conversations[indexPath.row].name 

    let alert = UIAlertController(title: alertname, message: message, preferredStyle: .alert) 

    let actionOK = UIAlertAction(title: "Play", style: UIAlertActionStyle.default, handler: { (action) in 
     //play the file 
    }) 

    let actionCancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: { (action) in 
     //cancel the file 
    }) 

    alert.addAction(actionOK) 
    alert.addAction(actionCancel) 

    self.present(alert, animated: true, completion: { 
     //some code here 
    }) 

我的第一个警报有一些延迟,但它大多是好的。但是如果我试图挖掘下一个细胞,我必须等待几秒钟才能显示警报。

所以,看起来我没有任何访问我的数据的问题(打印工作很好),但不知怎么的,它需要几秒钟后才显示UIAlertController。

我做错了什么?

+0

你在设备或模拟器测试它? –

+0

两者(结果相同)。 – lithium

+0

你可以尝试没有插件的设备。 –

回答

5

目前它在你的主队列,而不是:

DispatchQueue.main.async(execute: { 
    self.present(alert, animated: true, completion: { 
    //some code here 
    }) 
}) 
+0

哇。谢谢! 我会在9分钟内接受你的回复作为回答 – lithium

+0

太好了,很高兴帮助@Lithium –