9

I've just come across this new API on iOS8. I cannot however find any solution on if it is possible to use images instead of text and to allow left and right swipe? Is this even possible? The only implementation I've found is this:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *moreAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Button1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        // maybe show an action sheet with more options
        [self.tableView setEditing:NO];
    }];
    moreAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Button2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        [self.tableView setEditing:NO];
    }];
    moreAction2.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete"  handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    return @[deleteAction, moreAction, moreAction2];
}
MichiZH
  • 5,587
  • 12
  • 41
  • 81

3 Answers3

15

No, I don't think it's possible to have a right swipe (with a UITableViewCell, anyway). However, you can use an image for the button by setting its background color using colorWithPatternImage:. The width of the button seems to be determined by the length of the title, so if you want to change the width (and not have any visible title), use a title that consists of as many spaces as you need.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • 1
    this is a great tip, but when i try it, i get a tiled image. i can build image for size of row, but my cells have variable heights. if thats the case, is there any way around it or i should just use a third party library? – skinsfan00atg Feb 23 '15 at 21:15
  • 2
    For the images, I put together a CocoaPod that would try to solve this as elegantly as possible for the time being: https://github.com/benguild/BGTableViewRowActionWithImage – Ben Guild Aug 21 '15 at 16:53
5

in Swift it would be:

deleteAction.backgroundColor = UIColor(patternImage: UIImage(named:"sample")!)
theHellyar
  • 59
  • 3
0

When you are adding images to the swiped view, this answer may be helpful.

Isuru Jayathissa
  • 478
  • 4
  • 15