ios - UITableView 中带有警报的奇怪延迟

标签 ios swift uitableview uialertcontroller

我有一些 UITableView,其中包含来自内部 array 的数据。我想在点击时显示 UIAlertController,但我遇到了非常奇怪的延迟。

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。

我做错了什么?

最佳答案

改为将其显示在您的主队列中:

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

关于ios - UITableView 中带有警报的奇怪延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40083568/

相关文章:

ios - 有没有办法将字符串数组映射到图像数组?

ios - 我如何在 Collection View 中快速加载多个自定义 View

swift - 如何将 Xcode Assets 文件夹中的图像数组添加到 ViewController?

ios - 将 nib 文件添加到 ScrollView 时如何删除底部空白

ios - 类变量分配在 Alamofire 请求 block 内不起作用

ios - AVPlayerLayer 仅在边界更改时正确设置

TableView 单元格中的 iOS 布局约束冲突

c# - 将 UITableView 从列表切换到分组的最佳方法是什么?

iOS block 和同步性

ios - 如何检查沙箱服务器状态? ("Cannot connect to itunes store")