iPhone Programming: Set UITableView Accessory Arrow Style

The iPhone SDK provides an easy-to-code standard for creating consistent user interfaces. The UITableView class is used in many iPhone applications. Most applications that present information to you in rows that you can tap to slide a new page over to see details use a UITableView to do the magic. Once you have a UITableView setup, you can easily add the little accessory arrow icons to the right side of the table cells by following the steps in this Tech-Recipe.


At this point, you need to have a working UITableView-based application. If you aren’t at this point, you’ll need to take a few steps back and check with the official Apple iPhone SDK documentation.

The simplest case is adding an accessory icon to the right of all cells in the table. To do this, edit the source file of the table view you want to change and add the following method:

- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
   return UITableViewCellAccessoryDetailDisclosureButton;
}

Yeah, that’s a keyboard-full, but it’s not that bad. Because your table view class is the delegate of your actual table view, implementing this method of the UITableViewController class will automatically affect the cells in your table. No other changes are needed to make this work. Very cool.

If you don’t want to show the accessory image for every cell in the table, you’ll need to implement some logic in this method that takes the NSIndexPath parameter (which tells you which row in which section is being asked about) and responds appropriately.

There are three standard accessory images that can be used by this method. Each image is shown below with the matching UITableViewCellAccessoryType (the example uses shows a Disclosure indicator image because it returns UITableViewCellAccessoryDisclosureIndicator.

Disclosure indicator

UITableViewCellAccessoryDisclosureIndicator
Used to indicate that tapping the cell will slide in a new table view one level down in the hierarchy.


Detail disclosure button

UITableViewCellAccessoryDetailDisclosureButton
This button indicates that tapping the cell will reveal a detail view of that item.


Checkmark

UITableViewCellAccessoryCheckmark
Indicates that a row has been selected by the user.


Nothing

UITableViewCellAccessoryNone
This is the default value for the cell accessory indicating that no accessory should be shown.

This isn’t a brain surgery level Tech-Recipe and this information is, of course, covered in the official documentation, but I keep forgetting the method name and knew it would be easier to lookup here than in the docs. I hope someone else finds it helpful, too.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

  • seyont

    did you mean ‘UITableViewCellAccessoryDetailCheckmark’?

    • http://www.tech-recipes.com Quinn McHenry

      Whoo! Nice catch of my dysclipboardpastia syndrome. I fixed the recipe accordingly. Thank you!

  • Mr Burns

    Thanks it was more helpfull to google>Click this site> =Done! rather than go through the documentation

    -

  • Nekbeth

    Yeahh.. thanks a lot.. I’m very new to iphone development and I just could not find the way to implement a simple disclosure accessory arrow like this. Some books just may find it so easy or logical that they forget to explain it.

    Thanks :)

  • Romain

    “I hope someone else finds it helpful, too.”

    Yes, I did :-)
    Thanks

  • Rob

    Thanks! It may seem simple, but as others as have said, you site is easier to search / find than the documentation.

    Much appreciated!

  • Anonymous

    Thanks, saved some of my time instead of roaming around in the net in google engine

  • Kalle

    Just a note that this method is deprecated and will be invalid in future releases. *continues digging for non-deprecated solution* :) Thanks though, worked fine except for that deprecation part.

    -Kalle.

  • http://unlikelyteacher.com paul_sns

    Thanks!

  • Hippster

    Very helpful, just what i needed!!! :)

  • xiangxiang
  • Vpulsive

    Thanks so much!!!

  • http://www.intransitione.com Marco

    Unfortunately this solution is now deprecated.

    Use:

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    or whatever other indicator

    in
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

  • today

    thanksss

  • vijay

    Nice Post…it helped me….

  • Vlad Borovtsov

    Hey, seems that this method is deprecated.
    The better way is to use something this:

    - (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @”Cell”;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    [cell.textLabel setText:[availableLanguages objectAtIndex:[indexPath row]]];
    [cell.imageView setImage:[UIImage imageNamed:@"language_icon.jpg"]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    }