b2cloud

19th May 2012

Lists from Enums (with an iOS example)

Guides | Tutorial By 5 years ago

When you need to create any list in code there is a very easy way to make the list quickly reorderable. The solution is to use an enum, which is nearly a list in itself. Each entry in the enum represents a number, by default it is one more than the previous entry in the enum. When the order is just hardcoded, it can be time consuming when you need to move the entries of your list around. If you use an enum, then simply by changing the entry in the enum, your list will update automatically.

In my example I will use a UITableView on the iOS, but this same technique can be transferred to any programming language.

Create your enum. Most compilers will set your first entry’s value to be 0, but explicitly set it just in case. Make an entry in the enum for every item in the list, then add one more at the bottom representing the number of items in the enum. Because the first value is 0, as long as this count entry is kept last, it will always equal the number of objects in your list.

typedef enum
{
	TableRowCat = 0,
	TableRowDog,
	TableRowFish,
	TableRowSnail,
	TableRowMonkey,
	//	Leave this one last
	TableRow_COUNT
}
TableRow;

I won’t go through the basics on setting up a table and hooking it up to a dataSource, so I will just show how to setup the two dataSource methods required to show this in action.

In your tableView:numberOfRowsInSection: you can just return the enum count.

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return TableRow_COUNT;
}

In your tableView:cellForRowAtIndexPath: method setup a UITableViewCell. Before returning it do a switch case on the indexPath.row and handle each of the enum entries.

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString* identifier = @"cell";

	UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];

	if(cell == nil)
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];

	NSString* text = nil;

	switch(indexPath.row)
	{
		case TableRowCat:
			text = @"Cat";
			break;

		case TableRowDog:
			text = @"Dog";
			break;

		case TableRowFish:
			text = @"Fish";
			break;

		case TableRowSnail:
			text = @"Snail";
			break;

		case TableRowMonkey:
			text = @"Monkey";
			break;
	}

	[cell.textLabel setText:text];

	return cell;
}

Run the project and you have a nice ordered list. So this wasn’t much code but this method can be used in much more complicated scenarios making the code much simpler and easier to adjust. Go ahead and move an entry in the enum and you will see without any other code changes it ‘just works’. Adding a new entry is as easy as an entry into the enum and handling it in the switch case.

Download the end project here.

  • Tom

    That’s a sweet little tip, thanks! I wonder if it would be possible to rearrange an enum dynamically to reflect, say, a tableView with reorderable cells.

  • Tom

    To do this I’d recommend you start with the enum but instead add the data into an NSMutableArray, then use that array as your data source. When rearranged simply change the order in the array 🙂

Recommended Posts

Yammer integrations in ReactJS

Post by 5 years ago

I am writing this blog while I am working on a project for our client’s intranet website. The client requires the website has the ability to share, like and write comments in the website through

Got an idea?

We help entrepreneurs, organizations and established brands from around
the country bring ideas to life. We would love to hear from you!