Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在tableView中使用图像:编辑操作错误:_Ios_Swift_Uitableview_Uitableviewrowaction - Fatal编程技术网

Ios 在tableView中使用图像:编辑操作错误:

Ios 在tableView中使用图像:编辑操作错误:,ios,swift,uitableview,uitableviewrowaction,Ios,Swift,Uitableview,Uitableviewrowaction,我在使用UITableView时遇到了一些问题,更准确地说,就是在tableView中使用图像:EditActionsFrrowat: 下面是我的相关代码。可以说我在这里使用的图像都是70x70的正方形 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { var patternImg

我在使用UITableView时遇到了一些问题,更准确地说,就是在tableView中使用图像:EditActionsFrrowat:

下面是我的相关代码。可以说我在这里使用的图像都是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
}
事情是可行的,但不幸的是,只有到了一定程度。 查看此屏幕截图显示图像存在不必要的重复。显然,我希望每个按钮(图标)每次只出现一次


有人知道我需要如何修复代码以避免重复吗?

要避免重复,您必须根据需要增加宽度大小

    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视图


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

    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视图


在哪个操作系统中运行??iOS 11或更少?我使用的是iOS 11.2.2.Ok。chk和更新uI是否会更新我的ans。在哪个OS u r运行??iOS 11或更少?我使用的是iOS 11.2.2.Ok。chk和更新uI是否会更新我的ans。是。行得通,谢谢。我使用:width:UIScreen.main.bounds.width。但是我仍然没有很好的方法来设定高度;46.0只是碰巧在我当前的设备上工作,我不能使用行高(它是-1)。你的行高是自动的吗?UITableViewAutomaticDimension?我也尝试了UITableViewAutomaticDimension,但没有任何区别(行高始终为-1)。除了遵循您在回答中给出的(2)个指示(即“增加宽度”和“改变位置”),我确实得到了更好的图形结果。但触摸时,按钮似乎保持了原来的位置,换句话说,图像与触摸区域不再匹配。关于向上投票,我认为“接受答案”是最好的,但无论如何,我会按照你的意愿改变。我再次以新的形式重新表述了这个问题:是的。行得通,谢谢。我使用:width:UIScreen.main.bounds.width。但是我仍然没有很好的方法来设定高度;46.0只是碰巧在我当前的设备上工作,我不能使用行高(它是-1)。你的行高是自动的吗?UITableViewAutomaticDimension?我也尝试了UITableViewAutomaticDimension,但没有任何区别(行高始终为-1)。除了遵循您在回答中给出的(2)个指示(即“增加宽度”和“改变位置”),我确实得到了更好的图形结果。但触摸时,按钮似乎保持了原来的位置,换句话说,图像与触摸区域不再匹配。关于向上投票,我认为“接受答案”是最好的,但无论如何,我将根据您的喜好进行更改。我在这里再次以新的形式重新表述了问题: