use*_*462 30 uitableview ios swift xcode6 uitableviewrowaction
我做了一个自定义的UITableViewRowAction.现在我想添加一个图像而不是文本.我知道这是可能的,但不知道该怎么做.你们中的某些人是否知道如何在Swift中做到这一点并想帮助我?谢谢你的回答!
dim*_*iax 45
您需要将UIImage设置为行动的backgroundColor,具体来说是:
迅速:
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let askAction = UIContextualAction(style: .normal, title: nil) { action, view, complete in
print("Ask!")
complete(true)
}
// here set your image and background color
askAction.image = IMAGE
askAction.backgroundColor = .darkGray
let blockAction = UIContextualAction(style: .destructive, title: "Block") { action, view, complete in
print("Block")
complete(true)
}
return UISwipeActionsConfiguration(actions: [blockAction, askAction])
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.textLabel?.text = "row: \(indexPath.row)"
}
}
Run Code Online (Sandbox Code Playgroud)
Objective-C的:
UIColor(patternImage: UIImage(named: "IMAGE_NAME"))
Run Code Online (Sandbox Code Playgroud)
Pet*_*inz 10
Swift 4(iOS 11+):
iOS 11现在支持(仅)图像以显示在操作按钮中.您只需在表视图委托对象中初始化UISwipeActionsConfiguration对象:
extension MyTableViewController:UITableViewDelegate {
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .normal, title: nil, handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
debugPrint("Delete tapped")
success(true)
})
deleteAction.image = UIImage(named: "icon_delete.png")
deleteAction.backgroundColor = UIColor.red
return UISwipeActionsConfiguration(actions: [deleteAction])
}
Run Code Online (Sandbox Code Playgroud)
}
小智 7
我制作了这个简单的UITableViewRowAction类别,以便为我的动作设置图标.您可以设置图像,背景颜色,单元格高度(以管理动态单元格)和图标大小(以百分比表示).
extension UITableViewRowAction {
func setIcon(iconImage: UIImage, backColor: UIColor, cellHeight: CGFloat, iconSizePercentage: CGFloat)
{
let iconHeight = cellHeight * iconSizePercentage
let margin = (cellHeight - iconHeight) / 2 as CGFloat
UIGraphicsBeginImageContextWithOptions(CGSize(width: cellHeight, height: cellHeight), false, 0)
let context = UIGraphicsGetCurrentContext()
backColor.setFill()
context!.fill(CGRect(x:0, y:0, width:cellHeight, height:cellHeight))
iconImage.draw(in: CGRect(x: margin, y: margin, width: iconHeight, height: iconHeight))
let actionImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = UIColor.init(patternImage: actionImage!)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26025 次 |
| 最近记录: |