Hello Guys!

Make Dropdown list in Swift2.1

If you want to make drop down list in swift 2.1 then follow this steps.
And also i attached some screenshots to you easily understand actually in this blog what i am doing.

:: Screen Shots ::

 // Choose data to dropdown list :



// After choose data to dropdown list the dropdown menu in hide.



:: Start Coding ::



import UIKit
class DropDownViewController: UIViewController, UITableViewDelegate ,UITableViewDataSource{

// This are outlet for UI View in storyboard
   @IBOutlet weak var SelectSchool: UIButton!
   @IBOutlet weak var SelectSchoolView: UITableView!
  
// Create static data for display in drop down list
   var data : NSArray = []
  
   override func viewDidLoad() {
       super.viewDidLoad()

 // Set data in array is already created
       self.data = ["banana","apple","orange","xcode","programme"]
       
       self.SelectSchoolView.delegate = self
       self.SelectSchoolView.dataSource = self
       
  }

// You can remove it.
  override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
  }
  
// Witch rows you want to display in your drop down list
  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return self.data.count
   }

//  This is for your data in particular row
   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->    UITableViewCell
   {
       
           let TableIdentifier: String = "SimpleTableItem"
           var cell:UITableViewCell! = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "TableIdentifier")
       
           if cell == nil {
           cell = UITableViewCell(style: .Default, reuseIdentifier: TableIdentifier)
           }
           cell.textLabel!.text = self.data[indexPath.row] as? String
           return cell
   }
  
// This is for select row in drop down list
   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
       
      var cell: UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
       
       self.SelectSchool.setTitle(cell.textLabel?.text, forState: .Normal)
       self.SelectSchoolView.hidden = true
   }
  
// When select cell or data in drop down list at that time hide drop down list
  @IBAction func btnSelectSchool(sender: AnyObject) {
       
       if self.SelectSchoolView.hidden == true
       {
              self.SelectSchoolView.hidden = false
       }
       else
       {
              self.SelectSchoolView.hidden = true
       }
    }
}

Thank You!


Comments

Popular posts from this blog