UITableView Tutorial Part 3: ENABLING & CONFIGURING EDIT MODE

Hi Folks,

Hopes you are enjoying your weekend…

This is another tutorial related to UITableView,previously we have learned

1.Creating and using Simple tableView and populated data.

2. Creating UITableView using xib.

You can check Part 1 here and Part 2 here.

Some time you need to Edit your table view according to app .Let’s say you are dealing with some Music Player App, user must be able to delete songs from the list and thus UITableView requires Editing of Cells.

If you’re presenting data to your users in a tableView, it is very likely you would want to add & remove rows from the table. In order to do this, UITableViews have a BOOL property called isEditing, which you can use to enter & exit edit mode.

Edit Mode on a UITableView is configured through the

– (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath

and

– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath

Swipe-to-delete will be enabled by default if a tableView can be edited.

methods of the UITableView DataSource Protocol.Add these two method to part 2 tutorial source code.

Enabling Edit Mode

Return YES for all IndexPath Sections / Rows you want Edit Mode enabled for.

– (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath

{

// Return YES

returnYES;

}

Configuring Edit Mode – The TableView:

The following code will affect how  existing ones deleted.

– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

// Delete the row from the data source

[items removeObjectAtIndex:indexPath.row];

//reload Table view

[tableView reloadData];

}

Now hit run and swipe to delete the cell

Screen Shot 2013-01-27 at 3.10.01 PM

Screen Shot 2013-01-27 at 3.10.22 PMDone…Enjoy coding.. stay tune for more tutorial

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s