Reduce redundant code, cell copy menu

This commit is contained in:
relikd
2020-09-05 21:05:12 +02:00
parent b8660c9a35
commit d2fa67e0e3
6 changed files with 93 additions and 85 deletions

View File

@@ -90,3 +90,34 @@ extension EditActionsRemove where Self: UITableViewController {
func editableRowActions(_: IndexPath) -> [(RowAction, String)] { [(.delete, "Remove")] }
func editableRowActionColor(_: IndexPath, _: RowAction) -> UIColor? { nil }
}
// MARK: - Table Cell Tap Menu
struct TableCellTapMenu {
private var index: Int = Int.max
mutating func reset() { index = Int.max }
/// Create a new tap manu and shows it immediatelly. With optional buttons.
mutating func start(_ tableView: UITableView, _ indexPath: IndexPath, items: [UIMenuItem]? = nil) -> Bool {
let menu = UIMenuController.shared
if index == indexPath.row {
menu.setMenuVisible(false, animated: true)
reset()
return false
}
index = indexPath.row
let cell = tableView.cellForRow(at: indexPath)!
menu.setTargetRect(cell.bounds, in: cell)
menu.menuItems = items
menu.setMenuVisible(true, animated: true)
return true
}
/// Returns the item if the array index is in bounds.
func getSelected<T>(_ source: [T]) -> T? {
guard index < source.count else { return nil }
return source[index]
}
}