views:

6388

answers:

6

I'm curious if it's possible to intercept the default methods of 'Edit' mode on a UITableView. Typically you get a free 'delete' button if you side swipe a UITableViewCell that has delegate methods associated with it. I'd like to change the delete to some other, arbitrary selector. Instead of deleting the cell, I'd just like to run a hello world alert dialogue. Is something to this extent possible?

A: 

While I don't have an answer, I'd love to know this as well. Ideally, I'd like to restyle the Delete button with a different tint color (or background image, if that's how it's done) and different text.

I know this could be done manually, but recreating all of the edit-mode behavior would take a lot of work and probably isn't worth doing.

Marco
Re-doing it manually isn't as trivial in a UITableView either, because you need to intercept your classes instance in the responder chain, I believe. The TableView has its own touch API, so things like touchesBegan etc are not handled the same as a UIView.
Coocoo4Cocoa
+2  A: 

There is a property on UITableViewCell called editAction which is documented as letting you change the action used for insertion or deletion on individual cells (it uses the cell's target property too). I haven't tested it, but this sounds like it might do what you want.

Kevin Ballard
+1  A: 

I would implement a subclass of UITableViewCell and handle touch events in there. You would likely have to do all the animation stuff yourself, but I think it's probably the easiest solution. There's not a "Supported" method for changing the delete button, I don't think

Ed Marty
+3  A: 

Editing is implemented as a method on your UITableView’s delegate object. In your table controller, have whatever control activates editing call this:

[tableView setEditing: YES animated: YES];

Then, make sure that your delegate object implements this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
         UIAlertView *alert = [[UIAlertView alloc] 
    initWithTitle: @"Delete" 
    message: @"Do you really want to delete “George W. Bush”?" 
    delegate: self
    cancelButtonTitle: @"Cancel"
    otherButtonTitles: @"Of course!", nil];
 }
}

…or a more standard action might be:

[itemList removeObjectAtIndex:indexPath.row];
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
Thinkingman.com
Problem is what to do when the user clicks "cancel." The only thing that I've done that looks half okay is `reloadRowsAtIndexPaths` to get rid of the delete button.
Yar
A: 

How can we replace the Delete button with something else like "Copy" or something like that in order to offer a user to copy UITableView cell's content into the clipboard ?

JFMartin
A: 

@JFMartin and Marco - to replace the standard 'Delete' button use the following UITableview delegate method

  • (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
James