May 18, 2009

Swipe Delete From UITableView

You probably like to use swipe to delete elements from UITableView. Here you have a short example how to implement so in your iPhone applications.

On Swipe you receive the following message, you don't need to do anything in it:

-(void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
}

When a user hits DELETE you can process it:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If row is deleted, remove it from the list.
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// delete your data item here
// Animate the deletion from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

7 comments:

Tuong Huy said...

Hi there, can i ask a question ?
I wanna custom the text on delete button (incase swipe-to-delete), do you know how to do that?
Thanks in advance

Anar Manafov said...

thx for passing by!

If we don't talk here about localizations, then I think there is no any official way to do what you want. But you could try an unofficial one.

You know in the UITableViewCell there is a property _removeControl. You could try to use.

something like it is done here, I guess:

http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#F7b37LddSXY/Mobile/Views/CQFileTransferTableCell.m&q=UITableViewCell%20_removeControl

http://www.google.com/codesearch/p?hl=en&sa=N&cd=1&ct=rc#F7b37LddSXY/Mobile/Views/CQFileTransferTableCell.h&q=UITableViewCell%20_removeControl

Anyway, since this will change standard behavior of the control it may be problematic to pass AppStore review.

BTW, tell me whether it works at all.

Anar Manafov said...

sorry, in the previous comment the links were truncated.

Just go to
www.google.com/codesearch

and search for
CQFileTransferTableCell.h
and
CQFileTransferTableCell.m

Tuong Huy said...

Thanks for the information Anar, it's really helpful and it WORK like charm.
Sorry because the late response cause i went out during last weekend. Just review those code that you provided, try to implements this morning and it work.
It's really nice and glad to meet you and i'm just coding for learning purpose so that i dont care much about AppStore's review.Beside i just change the label of Delete button incase swipe-to-delete, is that can be called change standard behavior of the control ? I didnt know where to find the list of AppStore review criteria.
Hope that i can keep in touch with you. If you're using skype then my skype id is huynvt83.
My name's Huy, from VietNam, sorry because of my bad English :)

Anar Manafov said...

Nice to see that it helped. ;)
Concerning AppStore review, I believe when I assigned to the iPhone developer program in the agreement it was mentioned that I must not used hidden API.
I think everything what starts with the underscore sign "_" in Cocoa is for Apple internal use only.
So, I am not really sure whether they accept this implementation or not. I just don't know. :(

I hope to see you next time on my blog ;) I don't have much time to post here, but I am going to update it time-to-time.

Tuong Huy said...

Update:
- Only work on iphone OS 2.2 or 2.2.1. I've upgrade my test device (iphone) to iphone OS 3.0 and the delete button has appear again without changing the label. So that i stuck here, searching for more information but nothing news till now.
- Also stuck with MapKit, searching for solution to develop with MapKit on iPhone OS 2.2 or 2.2.1 but i found out that MapKit framework does not include in iPhone SDK 2.2 (at least on iPhone simulator private framework)

Anonymous said...

As of iPhone OS 3 you can also use the following

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath