Close every other open UITableViewCell
<p>I'm using Cristik's answer from this post: <a href="https://stackoverflow.com/questions/33186659/drop-down-list-in-uitableview-in-ios">Drop-Down List in UITableView in iOS</a></p>

<p>The code works fine, but I am trying to create logic so when the user clicks on a closed cell, every other open cell closes. So if the Account cell was open, when the user clicks on the Event one, the event one opens and the Account one closes. </p>

<p>I tried the following logic: </p>

<pre><code>for (var x = 0; x &lt; displayedRows.count; x++) {

                    let view = displayedRows[x]
                    if (view.isCollapsed == false &amp;&amp; x != indexPath.row){
                        let range = x+1...x+view.children.count
                        displayedRows.removeRange(range)
                        let indexPaths = range.map{return NSIndexPath(forRow: $0, inSection: indexPath.section)}

                        tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
                    }

                }
</code></pre>

<p>Here is the full function: </p>

<pre><code> override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: false)
    let viewModel = displayedRows[indexPath.row]
    if viewModel.children.count &gt; 0 {
        let range = indexPath.row+1...indexPath.row+viewModel.children.count
        let indexPaths = range.map{return NSIndexPath(forRow: $0, inSection: indexPath.section)}
        tableView.beginUpdates()
        if viewModel.isCollapsed {
            displayedRows.insertContentsOf(viewModel.children, at: indexPath.row+1)
            tableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)


            for (var x = 0; x &lt; displayedRows.count; x++) {

                let view = displayedRows[x]
                if (view.isCollapsed == false &amp;&amp; x != indexPath.row){
                    let range = x+1...x+view.children.count
                    displayedRows.removeRange(range)
                    let indexPaths = range.map{return NSIndexPath(forRow: $0, inSection: indexPath.section)}

                    tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
                }

            }

        }

        else {
            displayedRows.removeRange(range)
            tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
        }

        tableView.endUpdates()
    }
    viewModel.isCollapsed = !viewModel.isCollapsed
    lastIndex = indexPath.row
}
</code></pre>

<p>I've tried variations of this code as well but for some reason keep getting the following error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 6 into section 0, but there are only 6 rows in section 0 after the update'</p>

<p>I know that this is a datasource problem but I can't seem to find a solution. Any solution/or alternative way to accomplish this would be appreciated! Thanks </p>