如何在Table View中将图像添加到行动作?

如何在UITableview上从右侧滑动单元格时添加自定义图像以删除按钮,如下图所示?

在此处输入图像描述

  func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? { let remove = UITableViewRowAction(style: .Default, title: "\nRemove") { action, indexPath in } remove.backgroundColor = UIColor(patternImage: UIImage(named: "remove")!) } 

从上面的代码中可以看到Image,但它显示为堆叠在一起的一组图像。 我的意思是,我无法将"ContentMode"设置为".ScaleToAspectFit"用于"UIImage"

这种方法的最佳方法是什么?

示例代码可能很明显。

基本上,您正在绘制上下文,然后从上下文中检索新图像。

 let label = UILabel(frame: CGRect(x: 0, y: 0, width: 80, height: 100)) label.text = "ADD" label.textAlignment = .Center label.textColor = UIColor.whiteColor() let img: UIImage = UIImage(named: "delete") UIGraphicsBeginImageContextWithOptions(imgSize, false, UIScreen.mainScreen().scale) let context = UIGraphicsGetCurrentContext() // Set background color CGContextSetFillColorWithColor(context, UIColor.raangeRed().CGColor) CGContextFillRect(context, CGRect(x: 0, y: 0, width: 80, height: 80)) // Draw image img.drawInRect(CGRectMake(30, 15, 20, 20)) // Draw View label.layer.renderInContext(context!) // Retrieve new UIImage let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() let deleteRowAction = UITableViewRowAction(style: .Normal, title: " ") { (rowAction, indexPath) in // Do stuff } deleteRowAction.backgroundColor = UIColor(patternImage: newImage) 

您还可以创建自定义视图(使用UIImageView和UILabel)并在上下文中呈现它。

尝试这个

 let backImage = UIImageView(image: UIImage(named: "remove")) backImage.contentMode = .scaleAspectFit remove.backgroundColor = UIColor(patternImage:backImage.image!) 

选择-2

 var img: UIImage = UIImage(named: "remove") var imgSize: CGSize = tableView.frame.size UIGraphicsBeginImageContext(imgSize) img.drawInRect(CGRectMake(20, 0, 20, 20)) var newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() remove.backgroundColor = UIColor(patternImage: newImage) 

灵感来自Denny的回答。 这是他的代码的客观版本。

 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@" " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Do you really want to delete this comment?" message:@"" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; [alertView setTag:101]; [alertView show]; }]; UITableViewCell *commentCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; CGFloat height = commentCell.frame.size.height; UIImage *backgroundImage = [self deleteImageForHeight:height]; deleteAction.backgroundColor = [UIColor colorWithPatternImage:backgroundImage]; return @[deleteAction]; } 

以下是图像绘制方法:

 - (UIImage*)deleteImageForHeight:(CGFloat)height{ CGRect frame = CGRectMake(0, 0, 62, height); UIGraphicsBeginImageContextWithOptions(CGSizeMake(62, height), NO, [UIScreen mainScreen].scale); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); CGContextFillRect(context, frame); UIImage *image = [UIImage imageNamed:@"icon-delete"]; [image drawInRect:CGRectMake(frame.size.width/2.0, frame.size.height/2.0 - 10, 18, 20)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

结果是: 表视图单元格删除图像