这两个API似乎得到了相同的结果。在这种情况下,用一种比另一种更好?
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .destructive,
title: "Delete") { (action, indexPath) in
print("DELETE")
}
return [deleteAction]
}override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let DeleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: { (action, view, success) in
print("Delete")
})
DeleteAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [DeleteAction])
}发布于 2018-12-02 07:32:35
它的功能基本相同,但自iOS 11发布以来就可以使用swipe操作,并具有一些新特性:
action.image = UIImage(...)的图像。如果有足够的空间,它会显示图像和标题。
此外,您应该使用swipe,因为它们是首选的,而且在将来的更新中,UITableViewRowActions将被不推荐UITableView头注释可以告诉我们的方式:
使用-
tableView:trailingSwipeActionsConfigurationForRowAtIndexPath:代替此方法,该方法将在以后的发行版中被废弃。
发布于 2019-12-13 03:58:23
这是为我工作的代码
internal func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
let contextItem = UIContextualAction(style: .destructive, title: "") { (contextualAction, view, boolValue) in
// delete item at indexPath
//if self.isfilterenabled == true {
// return
//}
if self.isfilterenabled == true {
//entryFilter.filteredEntries[indexPath.row]
self.entryFilter.filteredEntries.remove(at: indexPath.row)
} else {
self.data.remove(at: indexPath.row)
}
self.table.deleteRows(at: [indexPath], with: .fade)
self.save()
}
//let share = UITableViewRowAction(style: .normal, title: "SavePDF") { (action, indexPath) in
// share item at indexPath
let contextItemSave = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in
let alert = UIAlertController(title: "Done! ", message: "PDF has been saved " ,preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK ", style: .default, handler: nil)
alert.addAction(okAction)
alert.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
// exclude some activity types from the list (optional)
//activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook ]
// present the view controller
self.present(alert, animated: true, completion: nil)
// present(alert, animated : true, completion : nil )
}
// share.backgroundColor = UIColor.blue
contextItemSave.image = UIImage(named:"PDF.jpg")
contextItem.image = UIImage(named:"delete.jpg")
let swipeActions = UISwipeActionsConfiguration(actions: [contextItem,contextItemSave])
return swipeActions
//return [delete, share]
}`发布于 2021-04-08 13:59:09
我已经为UISwipeActionsConfiguration创建了一个扩展,如果您的映像存在大小问题,可以使用它。基本上,我们的想法是从图像和文本创建属性化字符串,并将其设置为标签,并从该标签创建图像。并将其附加到UIContextualAction的image属性中。
extension UISwipeActionsConfiguration {
public static func makeTitledImage(
image: UIImage?,
title: String,
textColor: UIColor = .white,
font: UIFont = .systemFont(ofSize: 14),
size: CGSize = .init(width: 50, height: 50)
) -> UIImage? {
/// Create attributed string attachment with image
let attachment = NSTextAttachment()
attachment.image = image
let imageString = NSAttributedString(attachment: attachment)
/// Create attributed string with title
let text = NSAttributedString(
string: "\n\(title)",
attributes: [
.foregroundColor: textColor,
.font: font
]
)
/// Merge two attributed strings
let mergedText = NSMutableAttributedString()
mergedText.append(imageString)
mergedText.append(text)
/// Create label and append that merged attributed string
let label = UILabel(frame: CGRect(x: 0, y: 0, width: size.width, height: size.height))
label.textAlignment = .center
label.numberOfLines = 2
label.attributedText = mergedText
/// Create image from that label
let renderer = UIGraphicsImageRenderer(bounds: label.bounds)
let image = renderer.image { rendererContext in
label.layer.render(in: rendererContext.cgContext)
}
/// Convert it to UIImage and return
if let cgImage = image.cgImage {
return UIImage(cgImage: cgImage, scale: UIScreen.main.scale, orientation: .up)
}
return nil
}
}你可以这样使用它;
public func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration?
{
let deleteAction = UIContextualAction(
style: .normal,
title: nil,
handler: { [weak self] (_, _, success: (Bool) -> Void) in
success(true)
print("Your action in here")
}
)
deleteAction.image = UISwipeActionsConfiguration.makeTitledImage(
image: UIImage(named: "delete_icon"),
title: "Delete")
)
deleteAction.backgroundColor = .orange
return UISwipeActionsConfiguration(actions: [deleteAction])
}https://stackoverflow.com/questions/53577995
复制相似问题