Skip to content

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

rayastar edited this page Feb 7, 2015 · 1 revision

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]){
        result = [tableView
                  dequeueReusableCellWithIdentifier:MyCellIdentifier
                  forIndexPath:indexPath];
        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;
}

#import "ViewController.h"

static NSString *MyCellIdentifier = @"MyCells";

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@end

self.myTableView.delegate = self;
Clone this wiki locally