Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public protocol CellDelegate: class {}
  2.  
  3. public protocol UpdatableCell: class {
  4. var delegate: CellDelegate? { get set }
  5. }
  6.  
  7. public protocol FirstRespondableCell: class {
  8. var shouldBecomeFirstResponder: Bool { get set }
  9. func becomeFirstResponder()
  10. }
  11.  
  12. enum NotificationID {
  13. case wawa
  14. }
  15.  
  16. /// Protocol that needs to be implemented if the cell supports notification.
  17. protocol NotificationableCell: UpdatableCell {
  18.  
  19. var cellNotificationID: NotificationID { get set }
  20. var notificationToBeRemoved: NotificationID? { get set }
  21. }
  22.  
  23. /// Protocol that needs to be implemented by the objects that is managing the adding and removal of cells. Example ViewModel
  24. protocol NotificationRemovalObserver {
  25.  
  26. func removeNotification(with id: NotificationID)
  27.  
  28. }
  29.  
  30. public class CellDelegateTableDataSource: BaseTableViewDataSource {
  31.  
  32. var cellDelegate: CellDelegate
  33.  
  34. init(cellDelegate: CellDelegate, tableDataSource: BaseTableSource) {
  35. self.cellDelegate = cellDelegate
  36. super.init(vm: tableDataSource)
  37. }
  38.  
  39. override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  40. let cell = super.tableView(tableView, cellForRowAt: indexPath)
  41. (cell as? UpdatableCell)?.delegate = cellDelegate
  42. if let selectionStyle = (cell as? UnselectableCell)?.unselectableStyle {
  43. cell.selectionStyle = selectionStyle
  44. }
  45.  
  46. /// Removal of notification
  47. if let notificationableCell = cell as? NotificationableCell,
  48. let notificationRemovalObserver = notificationableCell.delegate as? NotificationRemovalObserver {
  49. if notificationableCell.cellNotificationID == notificationableCell.notificationToBeRemoved {
  50. notificationRemovalObserver.removeNotification(with: notificationableCell.cellNotificationID)
  51. }
  52. }
  53.  
  54.  
  55. return cell
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement