2017-06-18 63 views
-1

如何使用SWIFT 3來實現iOS中的下拉菜單圖如下圖所示: enter image description here如何使一個下拉菜單中的斯威夫特3

我尋覓SO問題,但他們更喜歡利用UIPicker的但我希望實現這一目標。是否可以通過使用表視圖來實現這種類型?

這給我需要從下拉菜單中選擇日期:

enter image description here

如何顯示在表視圖的日期,如下圖所示?

+0

您可以使用表創建下拉菜單。你可以找到很多Dropdown的演示。但我也會建議使用Picker來選擇日期而不是表格。 – Surjeet

回答

1

有一堆Demo和DropDown示例,當用戶單擊按鈕時,可以通過使用tableView只是tableview來實現此目的。 或者您可以使用此 https://cocoapods.org/pods/DropDown

let dropDown = DropDown() 

// The view to which the drop down will appear on 
dropDown.anchorView = view // UIView or UIBarButtonItem 

// The list of items to display. Can be changed dynamically 
dropDown.dataSource = ["Car", "Motorcycle", "Truck"] 

Optional properties: 

// Action triggered on selection 
dropDown.selectionAction = { [unowned self] (index: Int, item: String) in 
    print("Selected item: \(item) at index: \(index)") 
} 

// Will set a custom width instead of the anchor view width 
dropDownLeft.width = 200 
Display actions: 

dropDown.show() 
dropDown.hide() 
1

(SWIFT 3)添加文本框和uipickerview故事板再加入委託和數據源uipickerview並添加委託給文本框。按照視頻尋求幫助 https://youtu.be/SfjZwgxlwcc

import UIKit 

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate { 


@IBOutlet weak var textBox: UITextField! 
@IBOutlet weak var dropDown: UIPickerView! 


var list = ["1", "2", "3"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


public func numberOfComponents(in pickerView: UIPickerView) -> Int{ 
    return 1 

} 

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ 

    return list.count 

} 

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 

    self.view.endEditing(true) 
    return list[row] 

} 

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 

    self.textBox.text = self.list[row] 
    self.dropDown.isHidden = true 

} 

func textFieldDidBeginEditing(_ textField: UITextField) { 

    if textField == self.textBox { 
     self.dropDown.isHidden = false 
     //if you dont want the users to se the keyboard type: 

     textField.endEditing(true) 
    } 

} 
}