2017-01-09 6 views
2

テーブルセルをタップした後、警告表示が表示されるまでに4〜5秒の遅延があります。コードは次のとおりですUIAlertControllerからdelayを削除するには?

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

let cell = tableView.cellForRow(at: indexPath)! 
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert) 

let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in 
    let cell = tableView.cellForRow(at: indexPath)! 
    }) 
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in 
}) 
    alertController.addAction(ok) 
    alertController.addAction(cancel) 

present(alertController, animated: true, completion: nil) 
} 

この遅延を回避するにはどうすればよいですか?

+0

アニメーションは遅く見えますか? –

+0

いいえ、表示する必要のあるアクションメソッド、つまり「OK」、アラートコントローラの「キャンセル」です。 – leaner122

+0

あなたはどこのバックグラウンドスレッドですか? – Rikh

答えて

8

私たちはUIを扱うとき、それがメイントレッド上で行われなければならないことが重要です。 アラートとペーストを表示するコードをディスパッチメインスレッドブロックにコピーしてください。

DispatchQueue.main.async { 
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert) 

let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) -> Void in 
    let cell = tableView.cellForRow(at: indexPath)! 
    }) 
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in 
}) 
    alertController.addAction(ok) 
    alertController.addAction(cancel) 

present(alertController, animated: true, completion: nil) 
} 
+0

正しい....、それは多くの利点を提供します... –

+0

はそれを得ました。 that works – leaner122

+2

テーブルビューのデリゲートメソッドはメインキューでのみ呼び出す必要があるので、これは必要ではありません。 @ leaner122自分でdidSelectRowAtデリゲートメソッドを明示的に呼び出していますか? – rmaddy

0

UIAlertViewControllerを表示するメインキューのコードを記述します。

DispatchQueue.main.async { 
    //Write your code here. 
} 
+0

詳しいことはできますか? – leaner122

関連する問題