2017-06-27 86 views
5

我試圖在UITableView中添加圖像滑動樣式。我試着用表情文字&其做工精細如何在UITableViewRowAction中添加圖像?

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
    let editAction = UITableViewRowAction(style: .normal, title: "") { (rowAction, indexPath) in 
     print("edit clicked") 
    } 

    return [editAction] 
} 

但我需要的圖像,而不是表情,同時,我試過

editAction.backgroundColor = UIColor.init(patternImage: UIImage(named: "edit")!) 

但它越來越重複的圖像,我用圖像多種格式,如20 * 20 ,25 * 25,50 * 50但仍然重複。

如何添加圖片?

+0

[UITableViewRowAction圖像標題]的可能重複(https://stackoverflow.com/questions/29421894/uitableviewrowaction-image-for-title) – Pochi

+0

我相信它可能是一個與您嘗試設置的圖像大小有關的問題。請檢查[this](https://stackoverflow.com/a/37452825/4108415)瞭解更多信息。 –

+0

@傑克嘗試我的解決方案,我希望它解決您的問題。 –

回答

5

最後在iOS的11SWIFT 4我們可以在UITableView中的刷卡動作添加圖像與UISwipeActionsConfiguration

@available(iOS 11.0, *) 
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { 

      let action = UIContextualAction(style: .normal, title: "Files", handler: { (action,view,completionHandler) in 
       //do stuff 
       completionHandler(true) 
      }) 
     action.image = UIImage(named: "apple.png") 
     action.backgroundColor = .red 
     let confrigation = UISwipeActionsConfiguration(actions: [action]) 

     return confrigation 
    } 

WWDC video at 28.34

Apple Doc

注意的幫助:我用50 * 50分apple.png圖片加50個tableviewrow height

-1
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 

    let write = UITableViewRowAction(style: .default, title: "\u{1F58A}") { action, index in 
     print("edit button tapped") 
    } 

    return [write] 
} 

試試這個用unicode代替圖標。這將工作

0

這是我如何在objective-c中做到這一點,翻譯時應該在swift工作。

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *deleteString = @"Delete"; 
    CGFloat tableViewCellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath]; 
    UIImage *image = [UIImage imageNamed:@"delete_icon"]; 

    CGFloat fittingMultiplier = 0.4f; 
    CGFloat iOS8PlusFontSize = 18.0f; 
    CGFloat underImageFontSize = 13.0f; 
    CGFloat marginHorizontaliOS8Plus = 15.0f; 
    CGFloat marginVerticalBetweenTextAndImage = 3.0f; 

    float titleMultiplier = fittingMultiplier; 

    NSString *titleSpaceString= [@"" stringByPaddingToLength:[deleteString length]*titleMultiplier withString:@"\u3000" startingAtIndex:0]; 

    UITableViewRowAction *rowAction= [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:titleSpaceString handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     //Do Stuff 
    }]; 

    CGSize frameGuess=CGSizeMake((marginHorizontaliOS8Plus*2)+[titleSpaceString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:iOS8PlusFontSize] } context:nil].size.width, tableViewCellHeight); 

    CGSize tripleFrame=CGSizeMake(frameGuess.width*3.0f, frameGuess.height*3.0f); 

    UIGraphicsBeginImageContextWithOptions(tripleFrame, YES, [[UIScreen mainScreen] scale]); 
    CGContextRef context=UIGraphicsGetCurrentContext(); 

    [[UIColor blueColor] set]; 
    CGContextFillRect(context, CGRectMake(0, 0, tripleFrame.width, tripleFrame.height)); 

    CGSize drawnTextSize=[deleteString boundingRectWithSize:CGSizeMake(MAXFLOAT, tableViewCellHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize] } context:nil].size; 

    [image drawAtPoint:CGPointMake((frameGuess.width/2.0f)-([image size].width/2.0f), (frameGuess.height/2.0f)-[image size].height-(marginVerticalBetweenTextAndImage/2.0f)+2.0f)]; 

    [deleteString drawInRect:CGRectMake(((frameGuess.width/2.0f)-(drawnTextSize.width/2.0f))*([[UIApplication sharedApplication] userInterfaceLayoutDirection]==UIUserInterfaceLayoutDirectionRightToLeft ? -1 : 1), (frameGuess.height/2.0f)+(marginVerticalBetweenTextAndImage/2.0f)+2.0f, frameGuess.width, frameGuess.height) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:underImageFontSize], NSForegroundColorAttributeName: [UIColor whiteColor] }]; 

    [rowAction setBackgroundColor:[UIColor colorWithPatternImage:UIGraphicsGetImageFromCurrentImageContext()]]; 
    UIGraphicsEndImageContext(); 

    return @[rowAction]; 
} 
0

我發現SWIFT這樣做的一種方式3 -

enter image description here

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { 
     //let cell = tableView.cellForRow(at: indexPath) 
     //print(cell?.frame.size.height ?? 0.0)//hence we need this height of image in points. make sure your contentview of image is smaller 
     let deleteAction = UITableViewRowAction(style: .normal, title:"  ") { (rowAction, indexPath) in 
      print("delete clicked") 
     } 
     deleteAction.backgroundColor = UIColor(patternImage:UIImage(named: "delete")!) 
     return [deleteAction] 
    } 

我們需要確保我們的圖像尺寸與電池行高度 這裏匹配是我我用的圖像enter image description here

+0

正如我在'UITableViewRowAction'中發現的那樣,它在swift 3中具有有限的功能,但是您可以嘗試使用具有背景色的圖像。 – Jack