Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom edit view in UITableViewCell while swipe left. Objective-C or Swift

How to make a custom edit view in iOS7 UITableView with Objective C like the Evernote or the Apple Reminders app while swipe left. I have tried to set an custom editingAccessoryView, but this didn't work.

Evernote edit view:

enter image description here Reminders edit view:

enter image description here

My current code is

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {     return YES; }  - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {     if (editingStyle == UITableViewCellEditingStyleDelete) {         NSLog(@"delete");     } } 

I have tried to solve the problem with: (UITableViewController.h)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     //make cell      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];     [view setBackgroundColor:[UIColor greenColor]];     //add Buttons to view      cell.editingAccessoryView = view;      return cell; } 

And the same with: (UITableViewCell)

- (void)willTransitionToState:(UITableViewCellStateMask)state; - (void)setEditing:(BOOL)editing animated:(BOOL)animated; - (UIView*)editingAccessoryView; 
like image 957
ugoarangino Avatar asked Oct 03 '13 16:10

ugoarangino


People also ask

How do you customize swipe edit buttons in Uitableview?

As of iOS 8.0 there's an easy way to customize the list of buttons that appear when the user swipes from right to left: editActionsForRowAt . Return an array of UITableViewRowAction objects that have titles and styles (and also background colors if you want to customize their appearance), and iOS does the rest.

Can I use UITableViewCell as a view?

There is one way you can use contentView property of UITableViewCell which of type UIView . So simply get RestaurantTableViewCell as you previously getting from nib then use its contentView to get the UIView from it.


2 Answers

Just copy paste the code below!

-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {          UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Clona" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){        //insert your editAction here     }];     editAction.backgroundColor = [UIColor blueColor];          UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){        //insert your deleteAction here     }];     deleteAction.backgroundColor = [UIColor redColor];     return @[deleteAction,editAction]; } 
like image 120
Kartik 123 Avatar answered Sep 21 '22 23:09

Kartik 123


Swift 3

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {      let editAction = UITableViewRowAction(style: .normal, title: "Edit") { (rowAction, indexPath) in         //TODO: edit the row at indexPath here     }     editAction.backgroundColor = .blue      let deleteAction = UITableViewRowAction(style: .normal, title: "Delete") { (rowAction, indexPath) in         //TODO: Delete the row at indexPath here     }     deleteAction.backgroundColor = .red      return [editAction,deleteAction] } 

Swift 2.1

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {     let editAction = UITableViewRowAction(style: .Normal, title: "Edit") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in         //TODO: edit the row at indexPath here     }     editAction.backgroundColor = UIColor.blueColor()      let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in         //TODO: Delete the row at indexPath here     }     deleteAction.backgroundColor = UIColor.redColor()      return [editAction,deleteAction] } 

Note: for iOS 8 onwards

like image 23
MattyG Avatar answered Sep 23 '22 23:09

MattyG