Drop-Down List in Uitableview in iOS

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:

  1. 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).
  2. 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.
  3. 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:

  1. 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. When isExpanded is set, your cell could unhide the options (by changing constraints).
  2. 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



Leave a reply



Submit