【问题标题】:Swift DropDown using Cocoapods error, won't workSwift DropDown 使用 Cocoapods 错误,不起作用
【发布时间】:2019-11-25 09:53:19
【问题描述】:

我是 swift 新手,现在我正在尝试使用 cocoapods 在情节提要上创建一个下拉表。我已经按照 cocoapods 的所有教程进行操作,我猜我的代码有问题。 请在这里给我一个启发,为什么它在我声明下拉列表的地方不起作用。

谢谢你们的帮助,谢谢你们。

import iOSDropDown

class ViewController: UIViewController {

    @IBOutlet weak var DropDownMenu: DropDown!
    let dropDown = DropDown()
    let view = UIView()
    dropDown.anchorView = view
    dropDown.optionArray = ["option 1", "option 2", "option 3"]

【问题讨论】:

    标签: ios swift storyboard cocoapods


    【解决方案1】:
    override func viewDidLoad() {
    
        super.viewDidLoad()
    
        let dropDown = DropDown()
        let view = UIView()
        dropDown.anchorView = view // UIView or UIButton
    
        // For set direction .top, .bottom or .any(for automatic)
        dropDown.direction = .any 
        dropDown.optionArray = ["data 1", "data 2", "data 3"]
    
        // for custom cell you can set xib
        dropDown.cellNib = UINib(nibName: "MyCell", bundle: nil)
    
        // select item from dropdown
        dropDown.selectionAction = { [unowned self] (index: Int, item: String) in
           print("Selected item: \(item) at index: \(index)")
       }
       // if you want to set custom width then you can set.
       dropDownLeft.width = 200
    
       dropDown.show() // For Display Dropdown
       dropDown.hide() // For hide Dropdown
    }
    

    更多细节和功能可以参考https://github.com/AssistoLab/DropDown

    【讨论】:

      【解决方案2】:

      在viewDidLoad中写入初始化代码:

      override func viewDidLoad() {
          super.viewDidLoad()
      
          let dropDown = DropDown()
          let view = UIView()
          dropDown.anchorView = view
          dropDown.optionArray = ["option 1", "option 2", "option 3"]
      }
      

      【讨论】:

        【解决方案3】:
        import iOSDropDown
        
        class ViewController: UIViewController,UITextFieldDelegate {
        
        @IBOutlet weak var dropDown: DropDown!
        
        override func viewDidLoad() {
            super.viewDidLoad()
        
            // The list of array to display. Can be changed dynamically
            dropDown.optionArray = ["Option 1", "Option 2", "Option 3"]
            //Its Id Values and its optional
            dropDown.optionIds = [1,23,54,22]
        
            // The the Closure returns Selected Index and String
            dropDown.didSelect{(selectedText , index ,id) in
            print("Selected String: \(selectedText) \n index: \(index)")
                self.dropDown.hideList()
                self.dropDown.text = selectedText
            }
        }
        
        }
        
        func textFieldDidBeginEditing(_ textField: UITextField) {
                    dropDown.showList()
        }
        
        }
        

        你可以参考这个链接https://cocoapods.org/pods/iOSDropDown

        【讨论】:

          【解决方案4】:

          第 1 步:添加 Pod 并安装

          pod 'DropDown'
          

          第 2 步:在 UIButton Tap 中添加下拉菜单

          import UIKit
          import DropDown
          
          class ViewController: UIViewController {
          
          let dropDown = DropDown()
          
          override func viewDidLoad() {
          super.viewDidLoad()
          }
          
          @IBAction func tapChooseItems(_ sender: UIButton) {
          dropDown.dataSource = ["option 1", "option 2", "option 3"]
          dropDown.anchorView = sender
          dropDown.bottomOffset = CGPoint(x: 0, y: sender.frame.size.height)
          dropDown.show()
          dropDown.selectionAction = { [weak self] (index: Int, item: String) in
            guard let _ = self else { return }
            sender.setTitle(item, for: .normal)
             }
           }
          }
          

          第 3 步:在 UITableView 中添加下拉菜单

          import UIKit
          import DropDown
          
          class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
          
          @IBOutlet weak var tableView: UITableView!
          let dropDown = DropDown()
          
          override func viewDidLoad() {
           super.viewDidLoad()
           }
          
          func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          return 10
          }
          
          func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
          cell.textLabel?.text = "Result: \(indexPath.row+1): "
          return cell
          }
          
          func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          if let cell = tableView.cellForRow(at: indexPath) {
            dropDown.dataSource = ["option 1", "option 2", "option 3"]
            dropDown.anchorView = cell
            dropDown.bottomOffset = CGPoint(x: 0, y: cell.frame.size.height)
            dropDown.backgroundColor = .gray
            dropDown.show()
            dropDown.selectionAction = { [weak self] (index: Int, item: String) in
              guard let _ = self else { return }
              cell.textLabel?.text = "Result: \(indexPath.row+1): \(item)"
              }
             }
            }
           }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-10-13
            • 2014-06-09
            • 2018-12-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多