2015-04-02 89 views
26

我做了一个自定义的UITableViewRowAction。现在我想添加图片而不是文字。我知道这是可能的,但不知道如何去做。 您是否有人知道如何在Swift中做到这一点,并想帮助我? 感谢您的回答!UITableViewRowAction图像标题

+0

在这里找到答案-https://stackoverflow.com/questions/44771778/how-to-add-image-in-uitableviewrowaction/45301272#45301272 – Jack 2017-07-25 10:57:35

+0

更新回答两个iOS的11和早期 https://开头stackoverflow.com/questions/27740884/uitableviewrowaction-title-as-image-icon-instead-of-text/46337919#46337919 – 2017-09-21 07:50:27

回答

37

您需要的UIImage设置排行动的backgroundColor,具体是:

斯威夫特:

UIColor(patternImage: UIImage(named: "")) 

的Objective-C:

[UIColor colorWithPatternImage:[UIImage imageNamed:@""]]; 
+0

感谢它的工作! – user3592462 2015-04-03 15:58:33

+3

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

+0

@BenGuild,谢谢 – 2015-12-23 04:56:13

0
delActions.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"delete.png"]]; 
6
class TableViewRowAction: UITableViewRowAction 
{ 
    var image: UIImage? 

    func _setButton(button: UIButton) 
    { 
     if let image = image, let titleLabel = button.titleLabel 
     { 
      let labelString = NSString(string: titleLabel.text!) 
      let titleSize = labelString.sizeWithAttributes([NSFontAttributeName: titleLabel.font]) 

      button.tintColor = UIColor.whiteColor() 
      button.setImage(image.imageWithRenderingMode(.AlwaysTemplate), forState: .Normal) 
      button.imageEdgeInsets.right = -titleSize.width 
     } 
    } 
} 


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
{ 
    let delete = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "   ") { action, indexPath in } 
    delete.image = UIImage(named: "trashImg") 

    let sharing = TableViewRowAction(style: UITableViewRowActionStyle.Default, title: "   ") { action, indexPath in } 
    sharing.backgroundColor = UIColor.lightGrayColor() 
    sharing.image = UIImage(named: "sharingImg") 

    return [delete, sharing] 
} 
+0

仅供参考这不会让你只是添加一个图像,也必须有文字。当我将“”放入标题参数时(由于强制解开titleLabel.text),它崩溃了。我不得不把“”放入标题参数来获得一个只有滑动动作的图像......在正面,你可以通过空格的数量来控制按钮的宽度...... :) – Natalia 2016-09-09 18:57:49

+1

@Jayesh Miruliya,how你知道UITableViewRowAction有一个按钮属性?这不在文档中:https://developer.apple.com/reference/uikit/uitableviewrowaction。这在技术上是黑客? – etayluz 2016-09-19 16:02:02

+0

不适用于iOS 8.4 :( – Jenny 2016-09-23 04:29:37

0

问题与基本的图案颜色的方法是你的IM年龄需要与动作按钮的大小相同,或者至少在底部有一些更平坦的背景以防止图像重复(即使它将锚定在顶部,这并不是很好)。

我不得不处理动态高度的细胞,使我实现了以下内容:

- (UIColor *)backgroundImageForActionAtIndexPath:(NSIndexPath *)indexPath withImage:(UIImage *)image tintColor:(UIColor *)tintColor backgroundColor:(UIColor *)backgrounfColor expectedWith:(CGFloat)width { 
// 
CGRect cellFrame = [self.tableView rectForRowAtIndexPath:indexPath]; 
CGSize expectedSize = CGSizeMake(width, cellFrame.size.height); 

UIGraphicsBeginImageContextWithOptions(expectedSize, NO, 0.0); 

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
if (ctx) { 
    // set the background 
    CGContextSetFillColorWithColor(ctx, backgrounfColor.CGColor); 
    CGRect fillRect = CGRectZero; 
    fillRect.size = expectedSize; 
    CGContextFillRect(ctx, fillRect); 
    // put the image 
    CGContextSetFillColorWithColor(ctx, tintColor.CGColor); 
    CGRect rect = CGRectMake((expectedSize.width - image.size.width)/2., (expectedSize.height - image.size.height)/2., image.size.width, image.size.height); 
    [image drawInRect:rect]; 
} 

UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return [UIColor colorWithPatternImage:newImage]; 

}

你仍然需要设置expectedWidth所以你把行动的标题的空标签相匹配。例如:@“” - > 64.f

正如您所看到的,您可以在代码中设置按钮的背景和色调颜色,而不是在艺术品本身中。

1

我做了这个简单的UITableViewRowAction类别,以便为我的操作设置图标。 您可以设置图像,背景颜色,单元格高度(管理动态单元格)和图标大小百分比。

extension UITableViewRowAction { 

    func setIcon(iconImage: UIImage, backColor: UIColor, cellHeight: CGFloat, iconSizePercentage: CGFloat) 
    { 
    let iconHeight = cellHeight * iconSizePercentage 
    let margin = (cellHeight - iconHeight)/2 as CGFloat 

    UIGraphicsBeginImageContextWithOptions(CGSize(width: cellHeight, height: cellHeight), false, 0) 
    let context = UIGraphicsGetCurrentContext() 

    backColor.setFill() 
    context!.fill(CGRect(x:0, y:0, width:cellHeight, height:cellHeight)) 

    iconImage.draw(in: CGRect(x: margin, y: margin, width: iconHeight, height: iconHeight)) 

    let actionImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    self.backgroundColor = UIColor.init(patternImage: actionImage!) 
    } 
}