O'Reilly logo

iOS 5 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

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 ...

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