ComboBox like UITableViewCells

When I came across the Twitter App for iPhone, I saw something I immediately wanted to have for my own app: UITableViewCells acting like ComboBoxes, opening on tap, closing when a new option is chosen. The idea was so nice that I decided to write a small prototype which can now be found at a public github repository of mine. There is also some basic documentation that might be useful for those who want to re-implement this code.

The key facts in a nutshell:

  • You need a custom UITableViewCell in order to display a status indicator as shown in the “screenshot” above and to make it easier to maintain a status within the UITableViewCell itself. There are plenty of tutorials on the web demonstrating how to create those custom UITableViewCells and loading them from a XIB file.
  • Everytime you change the rowcount in a section (insert or delete cells) you need to adjust the number that ‘numberOfRowsInSection’ returns so you can check the state of your ComboBox cell and return the number of options in that cell plus 1 for the cell itself if the state is ‘open’. If you don’t adjust this value, you run into an inconsistency exception.
  • Although the option cells are not visible from the start, you need to return these cells from the ‘cellForRowAtIndexPath’ method. A good way to realize a dynamic number of option cells is to make use of the ‘default’ statement in the switch and only specify a static cell for the first row in that section (id 0).
            switch ([indexPath row]) {
                case 0: {
                 
                    // return the ComboBox cell here
                    
                    break;
                }
                default: {
                    
                    // return the Option cells here
                    
                    break;
                }
            }

If you’re interested, feel welcome to check the full prototype and it’s documentation in the repository.

  1. xcuze posted this

NIGHTNIGHT by DEDDY