swift - 为什么我需要在 UITableView 中的单元格上点击两次才能显示警报

标签 swift tableview uialertcontroller

我有一个简单的 tableView,我的想法是,当用户点击一个单元格警报时会弹出 - 但出于某种原因,我必须点击两次才能显示警报,这是为什么? 我的代码:

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

        let employee = employees[indexPath.row]

        let alertView = UIAlertController(title: "Stämpelklocka", 
            message: employee.firstName + " " + employee.lastName, 
            preferredStyle: .alert)
        let cancel = UIAlertAction(title: "Avbryt", style: 
            .destructive) { (action) in

        }
        let accept = UIAlertAction(title: "Stämpla Ut?", style: 
            UIAlertActionStyle.default) { (action) in
        }

        alertView.addAction(cancel)
        alertView.addAction(accept)
        present(alertView, animated: true, completion: nil)
}

最佳答案

你必须在这里取消选择它

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


    let employee = employees[indexPath.row]

    let alertView = UIAlertController(title: "Stämpelklocka", 
        message: employee.firstName + " " + employee.lastName, 
        preferredStyle: .alert)
    let cancel = UIAlertAction(title: "Avbryt", style: 
        .destructive) { (action) in

    }
    let accept = UIAlertAction(title: "Stämpla Ut?", style: 
        UIAlertActionStyle.default) { (action) in
    }

    alertView.addAction(cancel)
    alertView.addAction(accept)
    present(alertView, animated: true, completion: nil)

     // deselect 
    tableView.deselectRow(at: indexPath, animated: false)

 }

关于swift - 为什么我需要在 UITableView 中的单元格上点击两次才能显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832141/

相关文章:

ios - 如何将 scrollView 中的图像发送到 "detailViewController"?

ios - UIAlertController 缓慢出现和关闭 - Swift

ios - SwiftUI TextField 最大长度

ios - 固定标签在该位置且宽度相等

ios - 我如何在核心数据中保存图像然后检索它?使用 swift

binding - UITableView - 通过绑定(bind)更好地编辑?

class - 快速调用父类中的按钮点击方法

swift - 自定义 UITableViewCell 按钮调用不正确的目标

ios - 多行 UIAlertController 消息

ios - 如何更改 iOS 中的 UIAlertAction 按钮颜色?