Created
October 20, 2020 09:08
-
-
Save andreconghau/de574bdbb468e001c404a7270017bef5 to your computer and use it in GitHub Desktop.
Swipe to action TableViewCell ios 13. You can swipe to both right and left, and UIAlert if delete some one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MODEL Object Realm | |
import Foundation | |
import RealmSwift | |
@objcMembers class BookItem: Object { | |
enum Property: String { | |
case id, name, isCompleted | |
} | |
dynamic var id = UUID().uuidString | |
dynamic var name = "" | |
dynamic var isCompleted = false | |
override static func primaryKey() -> String? { | |
return BookItem.Property.id.rawValue | |
} | |
convenience init(_ name: String) { | |
self.init() | |
self.name = name | |
} | |
} | |
extension BookItem { | |
static func add(name: String, in realm: Realm = try! Realm()) -> BookItem { | |
let book = BookItem(name) | |
try! realm.write { | |
realm.add(book) | |
} | |
return book | |
} | |
// Get all item book | |
static func getAll(in realm: Realm = try! Realm()) -> Results<BookItem> { | |
return realm.objects(BookItem.self) | |
.sorted(byKeyPath: BookItem.Property.isCompleted.rawValue) | |
} | |
// Delete | |
func delete() { | |
guard let realm = realm else { return } | |
try! realm.write { | |
realm.delete(self) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import RealmSwift | |
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { | |
private var books: Results<BookItem>? | |
@IBOutlet weak var listBook: UITableView! | |
@IBOutlet weak var titleInput: UITextField! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
books = BookItem.getAll() | |
listBook.dataSource = self | |
listBook.delegate = self | |
} | |
@IBAction func addItemAction(_ sender: Any) { | |
guard let title = titleInput.text else { | |
return | |
} | |
if title == "" { | |
return | |
} | |
let item = BookItem.add(name: title) | |
titleInput.text = "" | |
print(item) | |
listBook.reloadData() | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return books?.count ?? 0 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "CELL", for: indexPath) as? BookTableViewCell | |
cell?.titleItem.text = self.books?[indexPath.row].name | |
return cell! | |
} | |
/* | |
SWIPE to Action | |
*/ | |
func tableView(_ tableView: UITableView, | |
editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { | |
return .none | |
} | |
// Right Swipe | |
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { | |
let action = UIContextualAction(style: .normal, | |
title: "Favourite") { [weak self] (action, view, completionHandler) in | |
self?.handleMarkAsFavourite() | |
completionHandler(true) | |
} | |
action.backgroundColor = .systemBlue | |
return UISwipeActionsConfiguration(actions: [action]) | |
} | |
func tableView(_ tableView: UITableView, | |
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { | |
// Archive action | |
let archive = UIContextualAction(style: .normal, | |
title: "Archive") { [weak self] (action, view, completionHandler) in | |
self?.handleMoveToArchive() | |
completionHandler(true) | |
} | |
archive.backgroundColor = .systemGreen | |
// Trash action | |
let trash = UIContextualAction(style: .destructive, | |
title: "Trash") { [weak self] (action, view, completionHandler) in | |
self?.handleMoveToTrash(book: (self?.books![indexPath.row]) as! BookItem) | |
completionHandler(true) | |
} | |
trash.backgroundColor = .systemRed | |
// Unread action | |
let unread = UIContextualAction(style: .normal, | |
title: "Mark as Unread") { [weak self] (action, view, completionHandler) in | |
self?.handleMarkAsUnread() | |
completionHandler(true) | |
} | |
unread.backgroundColor = .systemOrange | |
let configuration = UISwipeActionsConfiguration(actions: [trash, archive, unread]) | |
// If you do not want an action to run with a full swipe | |
configuration.performsFirstActionWithFullSwipe = false | |
return configuration | |
} | |
private func handleMarkAsFavourite() { | |
print("Marked as favourite") | |
} | |
private func handleMarkAsUnread() { | |
print("Marked as unread") | |
} | |
private func handleMoveToTrash(book: BookItem) { | |
print("Moved to trash") | |
print(book) | |
let alert = UIAlertController(title: "Hi!", message: "Bạn có muốn xóa \(book.name)", preferredStyle: .alert) | |
let ok = UIAlertAction(title: "Xóa", style: .default, handler: { action in | |
book.delete() | |
self.listBook.reloadData() | |
}) | |
alert.addAction(ok) | |
let cancel = UIAlertAction(title: "Hủy", style: .default, handler: { action in | |
}) | |
alert.addAction(cancel) | |
DispatchQueue.main.async(execute: { | |
self.present(alert, animated: true) | |
}) | |
} | |
private func handleMoveToArchive() { | |
print("Moved to archive") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment