简体   繁体   中英

Swift DropDown using Cocoapods error, won't work

i am new to swift and now i'm trying to create a dropdown table on storyboard using cocoapods. i have followed all the tutorial from the cocoapods & i guess there is something wrong with my code here. Please give me an enlightened here why it won't work where i have declared the dropdown.

Thankyou for helping, appreciate it guys.

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"]
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
}

for more detail and features you can refer https://github.com/AssistoLab/DropDown

Write the initialization code in viewDidLoad :

override func viewDidLoad() {
    super.viewDidLoad()

    let dropDown = DropDown()
    let view = UIView()
    dropDown.anchorView = view
    dropDown.optionArray = ["option 1", "option 2", "option 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()
}

}

you can refer this link https://cocoapods.org/pods/iOSDropDown

Step #1 : Add Pod and install

pod 'DropDown'

Step #2: Adding a drop-down in 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)
   }
 }
}

Step #3: Adding a drop-down inside 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)"
    }
   }
  }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM