【问题标题】:Strange delay with alert in UITableViewUITableView 中出现警报的奇怪延迟
【发布时间】:2016-10-17 10:07:17
【问题描述】:

我有一些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。

我做错了什么?

【问题讨论】:

  • 你是在设备中测试还是在模拟器中测试?
  • 两者(结果相同)。
  • 不带插件的设备可以试试吗?
  • 其实下面我们已经有了正确的答案。我试过了,它有效。

标签: ios swift uitableview uialertcontroller


【解决方案1】:

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

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

【讨论】:

  • 哇。谢谢!我会在 9 分钟内接受您的回复作为答复
  • 太好了,很高兴能帮助@lithium
猜你喜欢
  • 2011-04-06
  • 2017-10-28
  • 2012-01-01
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多