2
\$\begingroup\$

What I wanted to achieve is tableView with clickable cells, anytime somebody click a certain cell, another cell with picker, UITextField etc appears below, it's like a dropdown menu.

When browsing Stack Overflow, I saw many people suggest using tableView methods such insertRowsAtIndexPaths and deleteRowsAtIndexPaths.

That's fine, but afterwards I found out it requires huge amount of code to make it work (keep track of indexes in data source, add extra element to data source, again playing with changing indexes when deque more cells etc)

I end up with terribly ugly code (it's before refactoring though, sure one can make it better)

   func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3)
    {
        if self.menuOpen == false {



            self.tableView.beginUpdates()
            let tempIndexPath:NSIndexPath = NSIndexPath(forRow: indexPath.row + 1, inSection: 0)
            self.tableView.insertRowsAtIndexPaths([tempIndexPath], withRowAnimation: .Bottom)

            tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)



            data.append(["", ""])
            self.menuOpen = true
            self.menuOpenIndex = indexPath.row + 1
            self.tableView.endUpdates()

            let selection1IndexPath:NSIndexPath = NSIndexPath(forRow: 6, inSection: 0)
            let selection2IndexPath:NSIndexPath = NSIndexPath(forRow: 7, inSection: 0)
            let cell1 = self.tableView.cellForRowAtIndexPath(selection1IndexPath)
            let cell2 = self.tableView.cellForRowAtIndexPath(selection2IndexPath)
            cell1?.userInteractionEnabled = false
            cell2?.userInteractionEnabled = false
            print(data)


        }

        else
        {
            let tempIndexPath:NSIndexPath = NSIndexPath(forRow: self.menuOpenIndex, inSection: 0)
            //bug fixing
            let tempIndexPathToRetainSeparator:NSIndexPath = NSIndexPath(forRow: self.menuOpenIndex-1, inSection: 0)
            self.tableView.beginUpdates()

            let lowerCell:PickerCellTableViewCell = self.tableView.cellForRowAtIndexPath(tempIndexPath) as! PickerCellTableViewCell
            data[tempIndexPath.row-2][1] = "\(lowerCell.valuesDataSource[tempIndexPath.row-2][lowerCell.picker.selectedRowInComponent(0)])"
            self.tableView.deleteRowsAtIndexPaths([tempIndexPath], withRowAnimation: .Top)

            tableView.reloadRowsAtIndexPaths([tempIndexPathToRetainSeparator], withRowAnimation: .None)

            self.data.popLast()
            self.menuOpen = false
            //            self.tableView.reloadData()
            self.tableView.endUpdates()
            let selection1IndexPath:NSIndexPath = NSIndexPath(forRow: 5, inSection: 0)
            let selection2IndexPath:NSIndexPath = NSIndexPath(forRow: 6, inSection: 0)
            let cell1 = self.tableView.cellForRowAtIndexPath(selection1IndexPath)
            let cell2 = self.tableView.cellForRowAtIndexPath(selection2IndexPath)
            cell1?.userInteractionEnabled = true
            cell2?.userInteractionEnabled = true
            print(data)

        }

    }

    if (indexPath.row == 5 || indexPath.row == 6)
    {
        self.selectedIndex = indexPath.row
        self.tableView.reloadData()
        print(selectedIndex)
    }




}

Also, I found another method to do that, however it's even more tricky for me http://www.appcoda.com/expandable-table-view/

My question is:

What are your methods to build expandable tableView? Do you use some library/pod? Do you deal with coding everything the way I did? Or maybe there's easier, smarter and more concise way to do that?

\$\endgroup\$
  • \$\begingroup\$ "Tappable" cells. There's no clicking on iOS: there's no mouse. \$\endgroup\$ – Josh Caswell Jul 16 '16 at 3:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.