Add and delete customized UITableViewCell

Apple AppStore is a huge success. There is more than 400 thousands apps’s. So if you want to be noted is better you create a very interesting app. To do that in most cases you will want to customize the UI of your app. One very powerfull customization is change the tableview. You can do a lot of stuff changing the tableviewcell and probably you will want to put another UI elements inside the cell. In that case we may have a tricky situation when you have to add and remove customized cells from one tableview.

Animate the deletion from tableView

Apple provide an animated way to remove cells from tableView. You only have to remove the item you want from the collection that store tableview’s data (a NSMutableArray, for example) and then call deleteRowsAtIndexPaths method from tableview as follows:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.tableViewData count]>1) {
        // If row is deleted, remove it from the list.
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            [self.tableView beginUpdates];
            [self.tableViewData removeObjectAtIndex:indexPath.row];

            // Animate the deletion from the table.
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [self.tableView endUpdates];
        }
    }
}

Modifying layer that is being finalized

Imagine you have tableviews with cells that has textfields. And imagine you want to add and remove cells from this tableview. In that case you may have a problem with the cell deletion procedure. If you add and delete cells a few times probably you will receive this error message from the compiler:

GNU gdb 6.3.50-20050815 (Apple version gdb-1518) (Sat Feb 12 02:52:12 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 663.
2011-07-10 20:35:27.117 iScoreKeeper[663:207] modifying layer that is being finalized - 0x4e40450
Current language:  auto; currently objective-c

Why is it occur?

The tableView cell’s content are referred in two collections – into the collection that store tableview’s data and in the tableview itself. So, when you delete one cell the animated deletion procedure is executed in two steps: first it removes the object from the tableview model and second removes the cell from tableview.

In the case of our example, we have a NSMutableArray with textfields as cell’s content. When you remove the object (textfield) from the NSMutableArray it will receive a release message. So this tableviewcell will lost the reference of the textfield causing the crash.

The solution

Actually the solution is very simple. You only have to retain an reference of the object before you remove it from the NSMutableArray, then tableview will be able to do its job. See as follows:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.tableViewData count]>1) {
        // If row is deleted, remove it from the list.
        if (editingStyle == UITableViewCellEditingStyleDelete)
        {
            [self.tableView beginUpdates];
            // delete your data item here
            UITextField *tf = [self.tableViewData objectAtIndex:indexPath.row];
            [tf retain];
            [tf removeFromSuperview];
            [self.tableViewData removeObjectAtIndex:indexPath.row];

            // Animate the deletion from the table.
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [self.tableView endUpdates];
        }
    }
}

I hope it helps !

0 Responses to “Add and delete customized UITableViewCell”


Comments are currently closed.



Premium Wordpress Plugin