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

Problem

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.

Solution

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]){ static NSString *MyCellIdentifier = @"SimpleCell"; /* We will try to retrieve an existing cell with the given identifier */ result = [tableView dequeueReusableCellWithIdentifier:MyCellIdentifier]; if (result == nil){ /* If a cell with the given identifier does not exist, we will create the cell with the identifier and hand it to the table view */ result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier]; } result.textLabel.text = [NSString stringWithFormat:@"Section %ld, Cell %ld", (long)indexPath.section, (long)indexPath.row]; result.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; } return result; } - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 10; } - (void)viewDidLoad{ [super viewDidLoad]; self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.myTableView.dataSource = self; self.myTableView.delegate ...

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.