Expert iPhone app developers from India are sharing this tutorial with IOS developer’s community. They have intended this post to explain the bets implementation method to the novice. Through this post, other developers can learn how to implement custom dropdown list in Swift.
One liner: In this tutorial we will learn how to implement custom Dropdown list. For this tutorial we will create small dropdown with color names and selected color will be applied to application background view.
- Xcode 7+
- Swift 2.0
- iOS 8+
Start by creating one SingleViewApplication with product name SwiftDropDown. In language field select swift and devices field select universal.
To design user interface open Main.storyboard file. Add one UIButton on top of the main view to display selected color. Create required outlet for UIButton to ViewController.swift.
Drag one UITableView with same width of button and align it exactly at bottom of UIButton. Create required outlet for UITableView to ViewController.swift and also set delegate and dataSource. Please check below screenshot.
Click on prototype cell and set identifier as Cell. Please check below screenshot.
Now open ViewController.swift file and declare one NSMutableArray with some color names like Red, Yellow, Green etc. In viewDidLoad() method set border of tableview and button. At launchtimetableview will be hidden, all code is shown in below screenshot.
Next method is button click event method. In this method we handle table view hide and show. Check below screenshot.
Next are table view delegate and dataSource methods.
numberOfRowsInSection (): Set array count.
cellForRowAtIndexPath (): Set color names from array.
didSelectRowAtIndexPath (): In this method we change button title to the selected color Name and change view background color using select row and also hide table view. Please check below screenshot for more info.
Now build and run project. If everything is correct, your app should run properly and you will see below output. You can check dropdown list by clicking on button and select one from list.
This article was intended by iPhone app developers India with a purpose. They wanted to tell how a novice developer can implement custom dropdown list in swift. If there is any query, you can straightaway ask from experts through comments.