使用tableView:editActionsForRowAt中的图像: [英] Working with images in tableView:editActionsForRowAt:

查看:184
本文介绍了使用tableView:editActionsForRowAt中的图像:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在使用UITableView时遇到的一些麻烦,更确切地说是在tableView中使用图像:editActionsForRowAt:

Here is some trouble I am having working with a UITableView, more precisely using an image in tableView:editActionsForRowAt:

以下遵循我的相关代码.可能有用的说法是我在这里使用的图像都是70x70正方形.

Below follows my relevant code. It may be useful to say that the images I am using here are all square 70x70.

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

    let upBtn = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("upBtn button tapped")
    }
    patternImg = self.swipeCellImage(named: "UpIcn", side: 46.0)
    upBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let downBtn = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("downBtn button tapped")
    }
    patternImg = swipeCellImage(named: "DownIcn", side: 46.0)
    downBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let rmvBtn = UITableViewRowAction(style: .destructive, title: "") {
        action, index in
        print("rmvBtn button tapped")
    }
    patternImg = swipeCellImage(named: "TrashIcn", side: 46.0)
    rmvBtn.backgroundColor = UIColor(patternImage: patternImg!)

    return [downBtn,upBtn,rmvBtn]
}


func swipeCellImage(named name: String, side: CGFloat) -> UIImage? {
    let theImage = UIImage(named: name)
    UIGraphicsBeginImageContextWithOptions(CGSize(width: side*2, height: side), false, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()
    context!.setFillColor(UIColor.clear.cgColor)
    theImage?.draw(in: CGRect(x: 0, y: 0, width: side, height: side))
    let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return resultImage
}

一切正常,但不幸的是,只有到了一定程度. 观看此屏幕快照表明,存在不必要的图像重复现象.显然,我希望每个按钮(图标)每次只出现一次.

Things work, but unfortunately only up to a certain point. Looking at this screen shot shows that there is an unwanted repetion of the images. Clearly I want each button(Icon) to only appear one time each.

有人能看到我需要如何修复我的代码以避免这种重复?

Can anyone see how I need to fix my code to avoid this repetion?

推荐答案

为避免这种情况,您必须根据需要增加宽度大小.

To avoid that, you have to increase width size asper your requirement.

    UIGraphicsBeginImageContextWithOptions(CGSize(width:      self.view.frame.width, height: commonHei), false, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()

    context!.setFillColor(textColor.cgColor) // CHECK BY CHANGE BGCOLOR
    context!.fill(CGRect(x: 0, y: 0, width: (self.view.frame.width) / 3, height: commonHei)) // INCREASE WIDTH SIZE
    var img: UIImage = UIImage(named: "pause")!
    img.draw(in: CGRect(x: 0, y: 0, width: 30, height: 30)) // Change position as per ur requirement.
    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

如果您有更多疑问,请检查3D视图.

If you having more doubt, then check on the 3D view.

这篇关于使用tableView:editActionsForRowAt中的图像:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆