如何在UITableView的删除按钮上添加图片?

13

我有一个UITableView,我已经为它添加了编辑操作。现在我想在删除按钮标签上方添加图像,如下所示:

enter image description here

这是我的代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let blockAction = UITableViewRowAction(style: .Normal, title: "Block") { (rowAction:UITableViewRowAction, indexPath:NSIndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    blockAction.backgroundColor = UIColor.redColor()

    return [blockAction]
}

如何在我的删除按钮上添加图片?


@rmaddy,其他开发者是怎么做的? - Orkhan Alizade
可能他们正在使用第三方自定义表格视图单元格类,而不是使用UITableViewDelegate提供的标准API。 - rmaddy
1
请参考以下问题,您会找到答案:https://dev59.com/PmIj5IYBdhLWcg3waEf8
https://dev59.com/HF0b5IYBdhLWcg3wUP0X如果您使用的是iOS8+系统。
- sbsr
只有第二个链接是相关的。我忘记了使用“图案图像”作为背景颜色的想法。那可能行得通。 - rmaddy
2个回答

2
您可以在背景颜色上使用特殊的图像,并使用 '\n' 符号使文本向下移动。
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let blockAction = UITableViewRowAction(style: .normal, title: "\nBlock") { (rowAction:UITableViewRowAction, indexPath:IndexPath) -> Void in
        //TODO: Delete the row at indexPath here
    }
    blockAction.backgroundColor = UIColor(patternImage: UIImage(named: "delete_background")!)

    return [blockAction]
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接