如何在Table View中将图像添加到行动作? [英] How to add image to row action in Table View?
问题描述
如何在 UITableview
上向右滑动单元格时添加自定义图像以删除按钮,如下图所示?
How to add custom image to delete button when swiping cell from right on UITableview
as shown in the below image?
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")!) }
从上面的代码中可以看到图像,但它显示为堆叠在一起的图像组。我的意思是,我无法将ContentMode
设置为。ScaleToAspectFit
为UIImage
From the above code, Image is showing but, it is showing as a group of images stacked together. I mean, I am unable to set the "ContentMode"
to ".ScaleToAspectFit"
for the "UIImage"
这种方法的最佳方法是什么?
What is the best way for this approach?
示例代码可能很明显。
推荐答案
基本上,您正在绘制上下文,然后从上下文中检索新图像。 / p>
Essentially, you are drawing in a context, then retrieve the new image from the context.
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)并在上下文中呈现它。
You can also create a custom view (with an UIImageView and UILabel) and render it inside the context.
这篇关于如何在Table View中将图像添加到行动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!