2017-06-18 7 views
-1

でドロップダウンメニューを作成する方法:私はSOの質問を検索しました enter image description here以下の画像のように迅速3を使用したIOSでドロップダウンメニューを実装するためにどのようスウィフト3

が、彼らはUIPickerを利用することを好みます代わりに私はこれを達成したいと思います。テーブルビューを使用してこのタイプを実現することは可能ですか?

私に

そして、それは、ドロップダウンメニューから日付を選択する必要があります。

以下に示すように、テーブルビューで日付を表示する方法

enter image description here

+0

テーブルを使用してドロップダウンを作成できます。 Dropdownのデモがたくさんあります。しかし、私はまた、テーブルの代わりにピッカーを使って日付を選ぶことを提案します。 – Surjeet

答えて

1

ドロップダウンのデモとサンプルの束があります。ユーザーがボタンをクリックしたときにtableViewだけを使用してこれを実現できます。 またはあなたが、その後uipickerviewとテキストボックスに代理人を追加するために、デリゲートとデータソースを追加し、この 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

(スウィフト3)を使用してストーリーボードにテキストボックスと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) 
    } 

} 
} 
関連する問題