如何在UITableViewRowAction中添加图像?

我试图在UITableView滑动样式中添加图像。 我试着用Emoji文字和它的工作正常

 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,但仍然复制。

我如何添加图片?

最后,在iOS 11中SWIFT 4我们可以在UISwipeActionsConfiguration帮助下,在UITableView的滑动操作中添加图像

 @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 } 

WWDCvideo28.34

苹果文件

注意:我已经使用了50 * 50点的apple.png图片和50个tableview row height

这是我如何在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]; } 

我在SWIFT 3中find了一个这样做的方法 –

在这里输入图像说明

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

我们需要确保我们的图像尺寸与细胞行高度匹配这是我使用的图像 在这里输入图像说明

 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代替图标。 这将工作