2014-09-27 31 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