2014-09-27 87 views
8

我刚刚在iOS8上遇到了这个新的API。然而,我不能找到任何解决方案,如果可以使用图像,而不是文本,并允许左右刷卡?这甚至有可能吗?我发现的唯一的实现是这样的:iOS 8 UITableViewRowAction:向左滑动,图像?

- (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]; 
} 

回答

13

不,我不认为这是有可能有一个向右扫(有一个UITableViewCell,反正)。但是,您可以通过使用colorWithPatternImage:设置其背景颜色来为按钮使用图像。按钮的宽度似乎是由标题的长度决定的,所以如果您想更改宽度(并且没有任何可见的标题),请使用包含尽可能多空格的标题。

+0

谢谢主席先生:-) – MichiZH 2014-09-28 07:08:02

+1

这是一个伟大的小费,但是当我尝试它,我得到一个平铺图像。我可以为行的大小构建图像,但是我的单元格具有可变高度。如果是这种情况,有没有办法绕过它,或者我应该只使用第三方库? – skinsfan00atg 2015-02-23 21:15:24

+1

对于图片,我放了一个CocoaPod,试图尽可能优雅地解决这个问题:https://github.com/benguild/BGTableViewRowActionWithImage – 2015-08-21 16:53:40

5

斯威夫特这将是:

deleteAction.backgroundColor = UIColor(patternImage: UIImage(named:"sample")!) 
+1

我需要为图片设置多大的尺寸? – 2015-04-27 16:25:12

+10

这太糟糕了。它使图像在背景上重复出现。如果能告诉人们它做了什么本来是件好事。 – CarmenA 2015-05-14 15:58:38