FLEXTableViewCell.m 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // FLEXTableViewCell.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 4/17/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXTableViewCell.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXColor.h"
  11. #import "FLEXTableView.h"
  12. @interface UITableView (Internal)
  13. // Exists at least since iOS 5
  14. - (BOOL)_canPerformAction:(SEL)action forCell:(UITableViewCell *)cell sender:(id)sender;
  15. - (void)_performAction:(SEL)action forCell:(UITableViewCell *)cell sender:(id)sender;
  16. @end
  17. @interface UITableViewCell (Internal)
  18. // Exists at least since iOS 5
  19. @property (nonatomic, readonly) FLEXTableView *_tableView;
  20. @end
  21. @implementation FLEXTableViewCell
  22. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  23. {
  24. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  25. if (self) {
  26. UIFont *cellFont = UIFont.flex_defaultTableCellFont;
  27. self.titleLabel.font = cellFont;
  28. self.subtitleLabel.font = cellFont;
  29. self.subtitleLabel.textColor = [FLEXColor deemphasizedTextColor];
  30. self.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  31. self.subtitleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  32. }
  33. return self;
  34. }
  35. - (UILabel *)titleLabel {
  36. return self.textLabel;
  37. }
  38. - (UILabel *)subtitleLabel {
  39. return self.detailTextLabel;
  40. }
  41. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  42. {
  43. return [self._tableView _canPerformAction:action forCell:self sender:sender];
  44. }
  45. /// We use this to allow our table view to allow its delegate
  46. /// to handle any action it chooses to support, without
  47. /// explicitly implementing the method ourselves.
  48. ///
  49. /// Alternative considered: override respondsToSelector
  50. /// to return NO. I decided against this for simplicity's
  51. /// sake. I see this as "fixing" a poorly designed API.
  52. /// That approach would require lots of boilerplate to
  53. /// make the menu appear above this cell.
  54. - (void)forwardInvocation:(NSInvocation *)invocation
  55. {
  56. // Must be unretained to avoid over-releasing
  57. __unsafe_unretained id sender;
  58. [invocation getArgument:&sender atIndex:2];
  59. SEL action = invocation.selector;
  60. // [self._tableView _performAction:action forCell:[self retain] sender:[sender retain]];
  61. invocation.selector = @selector(_performAction:forCell:sender:);
  62. [invocation setArgument:&action atIndex:2];
  63. [invocation setArgument:(void *)&self atIndex:3];
  64. [invocation setArgument:(void *)&sender atIndex:4];
  65. [invocation invokeWithTarget:self._tableView];
  66. }
  67. - (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
  68. {
  69. if ([self canPerformAction:selector withSender:nil]) {
  70. return [self._tableView methodSignatureForSelector:@selector(_performAction:forCell:sender:)];
  71. }
  72. return [super methodSignatureForSelector:selector];
  73. }
  74. @end