iOS Tutorial: Selections in TableViews

This tutorial picks up where our last tutorial on TableView Basics left off. Now that we have one, it’s time to start editing, deleting and moving things around. Again, we’ll have our starter project and our completed project downloads.

First off, we can lose the button off the cell. Go to the storyboard and delete it. Delete the button code we had in the delegate method in RootViewController.m:

– (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPathSmilie: :(NSIndexPath *)indexPath


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@”tvcItems”];

UILabel *lblName = (UILabel *)[cell viewWithTag:100];

[lblName setText:[maTheData objectAtIndex: [indexPath row]]];

return cell;


Table View Styles

We’re going to go into our storyboard and play with some attributes to explore what we can make the UITableView look like, and act. First, select the table view, and go to the Attributes Inspector. Change the table view style to Grouped. Run the project again. This looks a lot like Apple’s Settings app.

Apple’s documentation has some guides to read more about tables, including styles making indexed lists and creating sections. This is also available from Xcode->Organizer-Documentation.

Selecting Cells

In order to edit a cell, you first must be able to select a row and have it mean something. When we run our project and click a cell, it simply turns blue and stays that way. Change the selection attribute to Multiple Selection, and run again. Now as you select, they all go blue. This is certainly okay, but a lot of people prefer checkboxes to indicate selections, without anything going blue. To do this, we change the cell style, and deselect the cell. Really, no kidding.

For this we have another UITableView delegate method called didSelectRowAtIndexPath:

– (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

UITableViewCell *tvc = [tableView cellForRowAtIndexPath:indexPath];

if ([tvc accessoryType] == UITableViewCellAccessoryCheckmark) {

[tvc setAccessoryType:UITableViewCellAccessoryNone];

} else {

[tvc setAccessoryType:UITableViewCellAccessoryCheckmark];


[tvc setSelected:NO];


Run the project, and notice that we are toggling the checkmark, and deselecting, which turns off our blue.

Category(s): Apple, iOS

Comments are closed.