dgelinas21
dgelinas21

Reputation: 641

create drop down in UITableView swift

I have gotten to the point where I have a selectable list of options, which are all loaded into the same .xib files. I am trying to make it so upon the click on a cell a different .xib file is populated below, and all of the other cells shift down. However, I am unsure of how to have 2 different xib's as TableViewCells in swift.

enter image description here

Upvotes: 0

Views: 3200

Answers (1)

Stephen
Stephen

Reputation: 1153

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
    }
    

    }

Upvotes: 1

Related Questions