Skip to content

Instantly share code, notes, and snippets.

@M-Miyazako
Created February 6, 2020 07:50
Show Gist options
  • Save M-Miyazako/81a54d334237e9288688ccf9d1d56463 to your computer and use it in GitHub Desktop.
Save M-Miyazako/81a54d334237e9288688ccf9d1d56463 to your computer and use it in GitHub Desktop.
# 削除可能なテーブルセル 要RxSwift
import RxSwift
import UIKit
/// セルの削除が可能なプロトコル
protocol CellRemovable: class {
var cellRemovedSubject: PublishSubject<Int> { get }
func removeConfiguration(at indexPath: IndexPath) -> UISwipeActionsConfiguration?
}
extension CellRemovable {
func removeConfiguration(at indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "削除") { [weak self] (_, _, completionHandler) in
do {
// TODO
completionHandler(true)
self?.cellRemovedSubject.onNext(indexPath.row)
self?.cellRemovedSubject.onCompleted()
} catch {
completionHandler(false)
}
}
let swipeAction = UISwipeActionsConfiguration(actions: [delete])
swipeAction.performsFirstActionWithFullSwipe = false
return swipeAction
}
}
final class HogeTableAdapter: UITableViewDelegate, CellRemovable {
func tableView(_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return removeConfiguration(at: indexPath)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment