O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4.2. Using Different Types of Accessories in a Table View Cell


You want to grab users’ attention in a table view by displaying accessories and offer different ways to interact with each cell in your table view.


Use the accessoryType of the UITableViewCell class, instances of which you provide to your table view in its data source object:

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

    UITableViewCell* result = nil;

    if ([tableView isEqual:self.myTableView]){

        result = [tableView

        result.textLabel.text =
        [NSString stringWithFormat:@"Section %ld, Cell %ld",

        result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;


    return result;


- (NSInteger) tableView:(UITableView *)tableView
    return 10;

- (void)viewDidLoad{
    [super viewDidLoad];

    self.myTableView = [[UITableView alloc]

    [self.myTableView registerClass:[UITableViewCell class]

    self.myTableView.dataSource = self;

    self.myTableView.autoresizingMask =
        UIViewAutoresizingFlexibleWidth |

    [self.view addSubview:self.myTableView];



You can assign any of the values defined in the UITableViewCellAccessoryType enumeration to the accessoryType property of an instance of the UITableViewCell class. Two very useful accessories are the disclosure indicator and the detail disclosure button. They both display a chevron indicating to users that if they tap on the associated table view cell, a new view or view controller will be displayed. In other words, the users will be taken to a new screen with further information about their current selector. The difference between these two accessories is that the disclosure indicator produces no event, whereas the detail disclosure button fires an event to the delegate when pressed. In other words, pressing the button has a different effect from pressing the cell itself. Thus, the detail disclosure button allows the user to perform two separate but related actions on the same row.

Figure 4-3 shows these two different accessories on a table view. The first row has a disclosure indicator, and the second row has a detail disclosure button.

Two table view cells with different accessories

Figure 4-3. Two table view cells with different accessories

If you tap any detail disclosure button assigned to a table view cell, you will immediately realize that it truly is a separate button. Now the question is: how does the table view know when the user taps this button?

Table views, as explained before, fire events on their delegate object. The detail disclosure button on a table view cell also fires an event that can be captured by the delegate object of a table view:

- (void)                        tableView:(UITableView *)tableView
 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    /* Do something when the accessory button is tapped */
    NSLog(@"Accessory button is tapped for cell at index path = %@", indexPath);

    UITableViewCell *ownerCell = [tableView cellForRowAtIndexPath:indexPath];

    NSLog(@"Cell Title = %@", ownerCell.textLabel.text);


This code finds the table view cell whose detail disclosure button has been tapped and prints the contents of the text label of that cell into the console screen. As a reminder, you can display the console screen in Xcode by selecting Run Console.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required