關於tableview 點擊 展示 UIAlertController 延遲緩慢的諸多理解 與解決方法

問題

    有時在點擊tableview上的一個cell後彈出UIAlertController,會發現有延遲的問題,或者點擊沒有反應,隨便再點擊一下才會彈出


問題解析 

   這種情況往往出現在 我們將 tableview 的 設置爲 cell.selectionStyle = UITableViewCellSelectionStyleNone,是因爲

  點擊事件發生後沒有處理UI變動,或者加到其他線程中了,主線程經過一次(或多次)循環後才發現此需要刷新UI,然後纔會刷新UI

 

解決辦法

1. 將 UIAlertController 放到 主線程中執行

func exit(){

        DispatchQueue.main.async {

            let alertController = UIAlertController(title: "提示",
                                                    message:"是否退出登錄?", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)

            let okAction = UIAlertAction(title: "確定", style: .destructive, handler: {
                action in

            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            NavigatorService.modelToPage(alertController, animated: true, completion: {})
        }
    }

2. 去掉  cell.selectionStyle = .none    在點擊事件裏用  tableView.deselectRow(at: indexPath, animated: false )來處理 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章