【问题标题】:Using buttons/images in tableView:editActionsForRowAt:在 tableView:editActionsForRowAt 中使用按钮/图像:
【发布时间】:2018-02-02 16:08:25
【问题描述】:

我正在尝试使用 tableView:editActionsForRowAt:,按钮带有图像而不是通常的文本。 但也存在一些挑战。有人可以帮我吗?

首先是我已经在做的工作——在 SOF 的一些宝贵帮助下——

这是我想要得到的理想状态:

相关代码如下。上面的第一种情况是在 xShift 和 xShiftInc 都设置为 0.0 时获得的。 第二种情况是通过将 xShift 初始化为 30.0 并将 xShiftInc 初始化为 20.0 获得的。

图形我得到了我想要的结果,但问题是按钮的触摸区域没有像图像一样移动。换句话说,在第二种情况下,如果我触摸垃圾箱,它会打印“upBtn touch!”。 让“rmvBtn感动!”打印我需要触摸垃圾桶的左侧。

另外,我对使用 46.0 作为单元格的高度不太满意,我想要一个更通用的参数(rowHeigh 等于 -1,所以不能使用)。但这是一个不同的问题。

func tableView(_ tableView: UITableView,
               editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    var patternImg:UIImage?, xShift = CGFloat(30.0)//CGFloat(0.0)//
    let xShiftInc = CGFloat(20.0)//CGFloat(0.0)//

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

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

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

    return [downBtn,upBtn,rmvBtn]
}


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

【问题讨论】:

  • 我更新了我的回答。如果您满意,请打勾并点赞。

标签: ios swift uitableview uitableviewrowaction


【解决方案1】:

简单的逻辑..!!手动我们不给 xshift 和 xshiftinc。

案例 1:

let downBtn = UITableViewRowAction(style: .normal, title: "") {
        action, index in
        print("downBtn touched!")
}

这里标题的文本是“”[没有空格的空字符串]

所以,tableview 自动为每个宽度取 30px。如果我们添加文本,它会自动增加它的宽度大小。

案例 2:

let downBtn = UITableViewRowAction(style: .normal, title: " ") {
        action, index in
        print("downBtn touched!")
}

这里标题的文本是“”[带1个空格的空字符串]

因此,tableview 自动为此采用 35px 的宽度。

希望你理解以上内容。

现在,我修改了你的代码。

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


    let cell = tableView.cellForRow(at: indexPath) as!      myTableViewCell

    print("cell_height    ", cell.frame.size.height) // U MAY GET ROW HEIGHT HERE

    var patternImg:UIImage?

    let downBtn = UITableViewRowAction(style: .normal, title: "  ") {
        action, index in
        print("downBtn touched!")
    }
    patternImg = swipeCellImage(named: "down", rowHeight: cell.frame.size.height)
    downBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let upBtn = UITableViewRowAction(style: .normal, title: "  ") {
        action, index in
        print("upBtn touched!")
    }

    patternImg = self.swipeCellImage(named: "up", rowHeight: cell.frame.size.height)
    upBtn.backgroundColor = UIColor(patternImage: patternImg!)

    let rmvBtn = UITableViewRowAction(style: .destructive, title: "  ") {
        action, index in
        print("rmvBtn touched!")
    }

    let patternIm = swipeCellImage(named: "trash", rowHeight: cell.frame.size.height)

    rmvBtn.backgroundColor = UIColor(patternImage: patternIm!)


    return [downBtn,upBtn,rmvBtn]
}


func swipeCellImage(name: String, rowHeight: CGFloat) -> UIImage? {

    let imgYposition : CGFloat = (rowHeight - 30) / 2

    // NOTE: This 30px is image height. Image height is always should be less than Rowheight.

    let theImage = UIImage(named: name)
    UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width,
                                                  height: rowHeight), false, UIScreen.main.scale)
    let context = UIGraphicsGetCurrentContext()
    context!.setFillColor(UIColor.yellow.cgColor)
    context!.fill(CGRect(x: 0, y: 0, width: (self.view.frame.width) / 3, height: side))

    theImage?.draw(in: CGRect(x: 5, y: imgYposition, width: 30, height: 30))

    // In this sample, am taking rowHeight as 44px.
    // Images should be 30 * 30 sizes.
    // I gave two spaces in title. So total width is 40px.
    // So,x = 5px and y = 7px.

    let resultImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()
    return resultImage
}

输出

如果您想在三个按钮之间留出更大的间距,只需增加标题的空文本并计算宽度并保持图像居中即可。

【讨论】:

  • 在您的 swipeCellImage 中,您在哪里定义边?你对我使用 rowHeight 它总是 -1 所以我不能使用它。
  • 可以分享一下你的屏幕设计怎么样?
  • 谢谢,你是对的;你的把戏奏效了。准确地说,我通过将图像的侧面设置为 (cell.frame.size.height+2.0) 的值来获得最佳结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 2015-10-13
相关资源
最近更新 更多