TableCell 删除行时的阴影效果
Shadow Effect While Deleting Row TableCell
我想在行下方添加这个阴影效果,同时滑动行删除。
我在我的控制器中显示删除按钮
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as NewNotificationTableViewCell
let delete = UITableViewRowAction(style: .normal, title: "Delete") { [self] action, index in
sectionArray[indexPath.section].items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
if sectionArray[indexPath.section].items.isEmpty {
sectionArray.remove(at: indexPath.section)
cell.addShadow()
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
}
}
delete.backgroundColor = UIColor.red
return [delete]
}
}
我认为最好改用 UIContextualAction
。它比旧的和弃用的 UITableViewRowAction
更容易定制。参考这篇文章:https://www.hackingwithswift.com/forums/ios/uitableview-swipe-actions-ios13-swift-5/2256
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return UISwipeActionsConfiguration(actions: [
makeDeleteContextualAction(forRowAt: indexPath)
])
}
//MARK: - Contextual Actions
private func makeDeleteContextualAction(forRowAt indexPath: IndexPath) -> UIContextualAction {
return UIContextualAction(style: .destructive, title: "Delete") { (action, swipeButtonView, completion) in
print("DELETE HERE")
action.image = ProjectImages.Annotation.checkmark
action.image?.withTintColor(.systemGreen)
action.backgroundColor = .systemOrange
//==> Put your shadow code here
completion(true)
}
}
使用 SwipeKit 通过以下代码解决了我的阴影问题。
extension NewNotificationViewController: SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
if orientation == .right {
let currentCell = tableView.cellForRow(at: indexPath)
currentCell?.contentView.addShadow(offset: CGSize.init(width: 0, height: 1), color: KSColor.bwBlack.getColor(), radius: 4.0, opacity: 0.10)
let delete = SwipeAction(style: .default, title: "enum.descriptor.delete".localized) { action, indexPath in
self.sectionArray[indexPath.section].items.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
if self.sectionArray[indexPath.section].items.isEmpty {
self.sectionArray.remove(at: indexPath.section)
self.tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
}
}
configure(action: delete, with: .delete)
return [delete]
} else {
return nil
}
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeTableOptions()
options.expansionStyle = .destructive(automaticallyDelete: false)
options.transitionStyle = defaultOptions.transitionStyle
options.maximumButtonWidth = 60
return options
}
func configure(action: SwipeAction, with descriptor: ActionDescriptor) {
action.title = descriptor.title(forDisplayMode: buttonDisplayMode)
action.backgroundColor = KSColor.red500Base.getColor()
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation) {
let currentCell = tableView.cellForRow(at: indexPath!)
currentCell?.contentView.addShadow(offset: CGSize.init(width: 0, height: 0), color: .clear, radius: 0.0, opacity: 0.0)
}
}
我也将我的手机类型更改为
class NewNotificationTableViewCell: SwipeTableViewCell
和委派自我
我想在行下方添加这个阴影效果,同时滑动行删除。
我在我的控制器中显示删除按钮
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as NewNotificationTableViewCell
let delete = UITableViewRowAction(style: .normal, title: "Delete") { [self] action, index in
sectionArray[indexPath.section].items.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
if sectionArray[indexPath.section].items.isEmpty {
sectionArray.remove(at: indexPath.section)
cell.addShadow()
tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
}
}
delete.backgroundColor = UIColor.red
return [delete]
}
}
我认为最好改用 UIContextualAction
。它比旧的和弃用的 UITableViewRowAction
更容易定制。参考这篇文章:https://www.hackingwithswift.com/forums/ios/uitableview-swipe-actions-ios13-swift-5/2256
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return UISwipeActionsConfiguration(actions: [
makeDeleteContextualAction(forRowAt: indexPath)
])
}
//MARK: - Contextual Actions
private func makeDeleteContextualAction(forRowAt indexPath: IndexPath) -> UIContextualAction {
return UIContextualAction(style: .destructive, title: "Delete") { (action, swipeButtonView, completion) in
print("DELETE HERE")
action.image = ProjectImages.Annotation.checkmark
action.image?.withTintColor(.systemGreen)
action.backgroundColor = .systemOrange
//==> Put your shadow code here
completion(true)
}
}
使用 SwipeKit 通过以下代码解决了我的阴影问题。
extension NewNotificationViewController: SwipeTableViewCellDelegate {
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
if orientation == .right {
let currentCell = tableView.cellForRow(at: indexPath)
currentCell?.contentView.addShadow(offset: CGSize.init(width: 0, height: 1), color: KSColor.bwBlack.getColor(), radius: 4.0, opacity: 0.10)
let delete = SwipeAction(style: .default, title: "enum.descriptor.delete".localized) { action, indexPath in
self.sectionArray[indexPath.section].items.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.automatic)
if self.sectionArray[indexPath.section].items.isEmpty {
self.sectionArray.remove(at: indexPath.section)
self.tableView.deleteSections(.init(integer: indexPath.section), with: .automatic)
}
}
configure(action: delete, with: .delete)
return [delete]
} else {
return nil
}
}
func tableView(_ tableView: UITableView, editActionsOptionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> SwipeOptions {
var options = SwipeTableOptions()
options.expansionStyle = .destructive(automaticallyDelete: false)
options.transitionStyle = defaultOptions.transitionStyle
options.maximumButtonWidth = 60
return options
}
func configure(action: SwipeAction, with descriptor: ActionDescriptor) {
action.title = descriptor.title(forDisplayMode: buttonDisplayMode)
action.backgroundColor = KSColor.red500Base.getColor()
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?, for orientation: SwipeActionsOrientation) {
let currentCell = tableView.cellForRow(at: indexPath!)
currentCell?.contentView.addShadow(offset: CGSize.init(width: 0, height: 0), color: .clear, radius: 0.0, opacity: 0.0)
}
}
我也将我的手机类型更改为
class NewNotificationTableViewCell: SwipeTableViewCell
和委派自我