【问题标题】:Working with images in tableView:editActionsForRowAt:在 tableView:editActionsForRowAt 中处理图像:
【发布时间】:2018-02-01 02:10:23
【问题描述】:

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

下面是我的相关代码。说我在这里使用的图像都是 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
}

一切正常,但不幸的是只达到了一定程度。 查看此屏幕截图显示图像存在不必要的重复。显然我希望每个按钮(图标)每次只出现一次。

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

【问题讨论】:

  • 您在哪个操作系统中运行?? iOS 11 或更低版本?
  • 我使用的是 iOS 11.2.2。
  • 好的。会检查并更新你
  • 我的ans更新了。

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

为避免这种情况,您必须根据需要增加宽度尺寸。

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

【讨论】:

  • 是的。这行得通,谢谢。我使用:宽度:UIScreen.main.bounds.width。不过,我仍然没有设置高度的好方法; 46.0 恰好适用于我当前的设备,我不能使用 rowHeight(它是-1)。
  • U r 行高自动? UITableViewAutomaticDimension?
  • 我也尝试过 UITableViewAutomaticDimension,但没有任何区别(rowHeight 始终为 -1)。除了遵循您在答案中提出的 (2) 指示(即“增加宽度”和“更改位置”),我确实得到了更好的图形结果。但是在触摸时,按钮似乎保持原来的位置,换句话说,图像不再与触摸区域匹配。关于赞成票,我认为“接受答案”是最好的,但无论如何,我会随你喜欢改变。
  • 我在这里再次以新的形式重新表述了这个问题:stackoverflow.com/questions/48586601/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多