【问题标题】:Displaying action sheet when tableview cell is tapped点击表格视图单元格时显示操作表
【发布时间】:2016-10-08 21:30:32
【问题描述】:

然后我试图在点击单元格时调用操作表,这就是我所做的

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

        let alertController = UIAlertController(title: "Action Sheet", message: "What do you like to do", preferredStyle: .alert)

        let okButton = UIAlertAction(title: "Done", style: .default, handler: { (action) -> Void in
            print("Ok button tapped")
        })

        let deleteButton = UIAlertAction(title: "Skip", style: .destructive, handler: { (action) -> Void in
            print("Delete button tapped")
        })

    alertController.addAction(okButton)
}

当我点击单元格时,警报控制器没有出现。我错过了什么?

【问题讨论】:

  • 一个方法不能访问在其他方法中声明的局部变量。为什么要在 viewWillAppear 中创建警报控制器?
  • 是的,对不起,我需要编辑我的问题
  • @rmaddy 请查看已编辑的问题
  • 究竟是什么不工作?
  • 当我点击单元格时,警报控制器没有出现

标签: ios swift uitableview


【解决方案1】:

你快到了,确保你也添加你的deleteButton-action,并使用present(alertController, animated: true, completion: nil)展示alertController

【讨论】:

  • 对不起,当然应该是 true 而不是是,在我的脑海中混淆了 Swift 和 Objective-C
【解决方案2】:

您的操作表没有显示,因为您没有展示它。

present(alertController, animated: true /** or false */, completion: nil)

【讨论】:

  • 感谢您的回复
【解决方案3】:

我们创建一个函数didSelectContact,并使用func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 来处理tableview 中选定的Cell。如果每个单元格都点按了此操作表,则会打开。

func didSelectContact(){

    let alert = UIAlertController(title: "Choose Once", message: "What you want do with contact", preferredStyle: UIAlertControllerStyle.actionSheet)
    let call = UIAlertAction(title: "Call", style: UIAlertActionStyle.default) { (UIAlertAction) in
    }
    let sms = UIAlertAction(title: "SMS", style: UIAlertActionStyle.default) { (UIAlertAction) in

    }
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)

    alert.addAction(call)
    alert.addAction(sms)
    alert.addAction(cancel)

    self.present(alert, animated: true, completion: nil)
}

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

    self.didSelectContact()
}

【讨论】:

  • 请说明您为帮助他人理解所做的工作。
  • 我们创建一个名为 didselectContct 的函数,然后我们使用 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 来处理 tableview 中选定的单元格,然后如果每个单元格都点击了此操作表,则会打开跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
相关资源
最近更新 更多