2016-10-17 69 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 –