我在 UITableView 中使用滑动来删除。在滑动中我需要添加图像。当我添加图像时,它会重复。如何停止重复
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
return
}
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
deleteButton.title = ""
return [deleteButton]
}
Problem
我需要的
正如您可能已经猜到的,问题就在这里:
deleteButton.backgroundColor = UIColor(patternImage: #imageLiteral(resourceName: "favourite_delete"))
您正在创建一个UIColor
使用(patternImage:)
初始化程序。这意味着图像将被重复(看到“图案”这个词了吗?)。
来自docs https://developer.apple.com/documentation/uikit/uicolor/1621933-init:
您可以使用图案颜色来设置填充或描边颜色,就像设置纯色一样。在绘制过程中,图案颜色的图像根据需要平铺以覆盖给定区域。
要解决此问题,您可以通过编程调整图像大小,使其适合按钮的框架。看调整 UIImage 大小的最简单方法? https://stackoverflow.com/questions/2658738/the-simplest-way-to-resize-an-uiimage
或者,使用MGSwipeTableViewCell https://github.com/MortimerGoro/MGSwipeTableCell,它允许您将图像放在滑动按钮上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)