【问题标题】:How to add UITableView in a UIAlertView in swift如何快速在 UIAlertView 中添加 UITableView
【发布时间】:2016-01-29 06:26:35
【问题描述】:

如何在 UIAlertView 中动态添加 UITableView。将 UIAlertView 的 subView 添加到 UITableView 后不显示。

 override func viewDidLoad() {
    super.viewDidLoad()
    tableView.frame         =   CGRectMake(0, 50, 320, 200);
    tableView.delegate      =   self
    tableView.dataSource    =   self
    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

@IBAction func textFieldCliked(sender: AnyObject) {
    alertView.message = "table view"
    alertView.addButtonWithTitle("Ok")
    alertView.addSubview(tableView)
    alertView.show()
}

【问题讨论】:

  • 我没有在这里添加 let alertView = UIAlertView() ..但是我已经在我的代码中添加了..仍然无法正常工作
  • 你不能这样做。只是为了在你的项目中添加按钮
  • 首先,UIAlertView 在 iOS 8 中已被弃用,因此您可能应该看看UIAlertController。其次,警报并非旨在包含自定义子视图(如 TableView)。考虑以模式显示您的表格,因为警报旨在允许用户从一些可用操作中进行选择。

标签: ios swift uitableview uialertview


【解决方案1】:

在 Swift 中创建 UITableView:

  1. 创建一个 UITableViewController 类。

  2. 使用必要的代码填充其代表(行数、didSelect、cellForRowAtIndexPath 等)。

  3. 现在在AlertViewController 中调用它的一个实例。

  4. 将所需数据传递给 TableView(如行数和内容数组)。

  5. 将 TableView 实例添加到警报视图。

我的代码片段:

let alertController : UIAlertController = UIAlertController(title: "Select email ID", message: nil, preferredStyle: .Alert)
alertController.view.backgroundColor = UIColor.whiteColor()
alertController.view.layer.cornerRadius = 8.0

let contactNumVC = contactNumberTableViewController ()

contactNumVC.count = emailIDs.count

contactNumVC.numbersArray = emailIDs

contactNumVC.preferredContentSize = CGSizeMake(alertController.view.frame.width, (44 * CGFloat(emailIDs.count)))

alertController.setValue(contactNumVC, forKeyPath: "contentViewController")

【讨论】:

    【解决方案2】:

    我不认为你可以用UIAlertView 做到这一点,但你可以制作一个小视图并以模态方式呈现它或类似UIPopoverPresentationController

    【讨论】:

      【解决方案3】:

      为了在应用程序的多个位置使用带有表格/图像或自定义按钮操作的警报类型行为,我创建了一个包含所有这些示例的示例项目 Generic popover。这不使用转换委托。 该项目还演示了自定义字体和颜色以适应您的应用主题。

      在情节提要中创建一个自定义的 tableview/imageview/buttons 并调用如下

      if let vc = self.storyboard?.instantiateViewController(withIdentifier: "TableContentViewController") as? TableContentViewController{
                  vc.modalPresentationStyle = .overCurrentContext
                   vc.popOverDelegate = self
                  self.present(vc, animated: false, completion: nil)
              }
      

      链接中的更多信息 https://github.com/shruezee/GenericPopovers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-04
        相关资源
        最近更新 更多