Drop-Down List in UITableView in iOS
You could have Account as a cell that expands on tap to reveal three buttons ("Profile", "Activate Account", "Change Password"), but that creates a problem: tapping around each of the three buttons will count as "user selected the Account cell" and trigger -tableView:didSelectRowAtIndexPath:
with the resulting expand/collapse of the cell.
Or you could make each of the hidden options ("Profile", "Activate Account", "Change Password") a separate table view cell. But I don't know how you could animate the three cells as a whole expanding and contracting (instead of each expanding separately from zero height to fully expanded).
So, perhaps the best solution is to:
- Have the even cells (indices: 0, 2, 4...) to fulfil both the role of "Menu title" and "Toggle menu open/close" (towards the associated odd cells described below).
- Interleave the (initially collapsed) "menu body" cells, each with one button per option (e.g. "Profile", "Activate Account", "Change Password"), laid out vertically, in the odd indices (1, 3, 5...). Use target-action to respond to the user selecting each option/button.
- Implement the table view delegate method so that only the even cells (menu headers) are selectable, and implement selection logic to expand/collapse the corresponding odd cell (inside -tableView:didSelectRowAtIndexPath:). For example, selecting the cell at index 0 ("Account") results in expanding/collapsing the cell at index 1 (menu with options "Profile", "Activate Account", "Change Password").
It is not the most elegant use of UITableView, but will get the job done.
create drop down in UITableView swift
There's two ways I can think of to accomplish what you are trying to do:
- Single xib file: You should use a single xib file that hides the lower section that contains the options until the cell is tapped. When tapped, a property on the UITableCell class could be set, e.g.
isExpanded
. WhenisExpanded
is set, your cell could unhide the options (by changing constraints). Multiple xibs: When tapped, you could insert a new item into your datasource. In your cellForRow, you could check for some identifier and load either the regular cell or the options cell using something like this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == optionsRow {
let cell = tableView.dequeueReusableCell(withIdentifier: OptionsCellIdentifier, for: indexPath) as! OptionsCell
//Configure cell here
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: NormalCellIdentifier, for: indexPath) as! NormalCell
//Configure cell here
return cell
}}
Dropdown list in Table view
I have used the subtable master which acts as drop down in table view itself.attached link below
https://github.com/ajkoshy7/SubTable
Adding Drop-down menu iOS
I solved the problem by creating a hidden tableview under textfield in storyboard!
Related Topics
Swift 2.0 Calendar Components Error
iOS 7 App Icons, Launch Images and Naming Convention While Keeping iOS 6 Icons
Uiscrollview: Paging Horizontally, Scrolling Vertically
How to Load a Xib File in a Uiview
Custom Uitableview Section Index
How to Fix Xcode "Dtassetproviderservice Could Not Start.." Error
Getting Local Notifications to Show While App Is in Foreground Swift 3
How to Get an Iso 8601 Date on iOS
How to Open File and Append a String in It, Swift
Uilabel Layer Cornerradius Negatively Impacting Performance
Custom Mkannotation Callout Bubble with Button
Round Top Corners of a Uibutton in Swift
Cocoapods - 'Pod Install' Takes Forever