UITableView+MATableViewAdPlacer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // UITableView+MATableViewAdPlacer.h
  3. // AppLovinSDK
  4. //
  5. // Created by Ritam Sarmah on 2/22/22.
  6. //
  7. #import <UIKit/UIKit.h>
  8. @class MATableViewAdPlacer;
  9. NS_ASSUME_NONNULL_BEGIN
  10. /**
  11. * When using an @c MATableViewAdPlacer, you should replace the usage of the original @c UITableView properties/methods in your code with the prefixed version from this category.
  12. * These methods help simplify your application logic by automatically returning "original" index paths (by accounting for added ad index paths in the stream) as well as serving to inform the ad placer of changes to its associated table view.
  13. */
  14. @interface UITableView (MATableViewAdPlacer)
  15. /**
  16. * The ad placer associated with this collection view.
  17. *
  18. * This value is automatically set when initializing @c MATableViewAdPlacer.
  19. */
  20. @property (nonatomic, weak, nullable) MATableViewAdPlacer *adPlacer;
  21. /**
  22. * The object that acts as the original data source of the collection view.
  23. *
  24. * Initializing an @c MATableViewAdPlacer will replace the original data source. This property allows access to the original data source object if needed.
  25. */
  26. @property (nonatomic, weak, nullable, setter=al_setDataSource:) id<UITableViewDataSource> al_dataSource;
  27. /**
  28. * The object that acts as the original delegate of the collection view.
  29. *
  30. * Initializing an @c MATableViewAdPlacer will replace the original delegate. This property allows access to the original delegate object if needed.
  31. */
  32. @property (nonatomic, weak, nullable, setter=al_setDelegate:) id<UITableViewDelegate> al_delegate;
  33. #pragma mark - Recycling Table View Cells
  34. /**
  35. * Returns a reusable table-view cell object for the specified reuse identifier and adds it to the table.
  36. * @param identifier A string identifying the cell object to be reused. This parameter must not be nil.
  37. * @param indexPath The index path specifying the location of the cell. Always specify the index path provided to you by your data source object. This method uses the index path to perform additional configuration based on the cell’s position in the table view.
  38. */
  39. - (__kindof UITableViewCell *)al_dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
  40. #pragma mark - Getting Cells and Section-Based Views
  41. /**
  42. * The non-ad table cells that are visible in the table view.
  43. */
  44. @property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *al_visibleCells;
  45. /**
  46. * An array of index paths, each identifying a visible non-ad row in the table view.
  47. */
  48. @property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *al_indexPathsForVisibleRows;
  49. /**
  50. * Returns the table cell at the index path you specify.
  51. *
  52. * @param indexPath The index path locating the row in the table view.
  53. *
  54. * @return The cell object at the corresponding index path. In versions of iOS earlier than iOS 15, this method returns @c nil if the cell isn’t visible or if @c indexPath is out of range. In iOS 15 and later, this method returns a non-nil cell if the table view retains a prepared cell at the specified index path, even if the cell isn’t currently visible.
  55. */
  56. - (nullable __kindof UITableViewCell *)al_cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  57. /**
  58. * Returns an index path that represents the row and section of a specified table view cell.
  59. *
  60. * @param cell A cell object of the table view.
  61. *
  62. * @return An index path representing the row and section of the cell, or @c nil if the index path is invalid or is for an ad row.
  63. */
  64. - (nullable NSIndexPath *)al_indexPathForCell:(UITableViewCell *)cell;
  65. /**
  66. * Returns an index path that identifies the original row and section at the specified point.
  67. *
  68. * @param point A point in the local coordinate system of the table view (the table view’€™s bounds).
  69. *
  70. * @return An index path representing the row and section associated with point, or @c nil if the point is out of the bounds of any row.
  71. */
  72. - (nullable NSIndexPath *)al_indexPathForRowAtPoint:(CGPoint)point;
  73. /**
  74. * Returns an index path that identifies the row and section at the specified point.
  75. *
  76. * @param rect A rectangle defining an area of the table view in local coordinates.
  77. *
  78. * @return An array of @c NSIndexPath objects each representing a row and section index identifying a non-ad row within @c rect. Returns an empty array if there aren’t any rows to return.
  79. */
  80. - (nullable NSArray<NSIndexPath *> *)al_indexPathsForRowsInRect:(CGRect)rect;
  81. #pragma mark - Selecting Rows
  82. /**
  83. * An original index path that identifies the row and section of the selected row.
  84. */
  85. @property (nonatomic, readonly, nullable) NSIndexPath *al_indexPathForSelectedRow;
  86. /**
  87. * The original index paths that represent the selected rows.
  88. */
  89. @property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *al_indexPathsForSelectedRows;
  90. /**
  91. * Selects a row in the table view that an index path identifies, optionally scrolling the row to a location in the table view.
  92. *
  93. * @param indexPath An index path identifying a row in the table view.
  94. * @param animated @c YES if you want to animate the selection and any change in position; @c NO if the change should be immediate.
  95. * @param scrollPosition A constant that identifies a relative position in the table view (top, middle, bottom) for the row when scrolling concludes. See @c UITableViewScrollPosition for descriptions of valid constants.
  96. */
  97. - (void)al_selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
  98. /**
  99. * Deselects a row that an index path identifies, with an option to animate the deselection.
  100. *
  101. * @param indexPath An index path identifying a row in the table view.
  102. * @param animated @c YES if you want to animate the deselection, and @c NO if the change should be immediate.
  103. */
  104. - (void)al_deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
  105. #pragma mark - Inserting, Deleting, and Moving Rows
  106. /**
  107. * Inserts rows in the table view at the locations that an array of index paths identifies, with an option to animate the insertion. Notifies the associated ad placer.
  108. *
  109. * @param indexPaths An array of index path objects, each representing a row index and section index that together identify a row in the table view.
  110. * @param animation A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See @c UITableViewRowAnimation for descriptions of the constants.
  111. */
  112. - (void)al_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
  113. /**
  114. * Deletes the rows that an array of index paths identifies, with an option to animate the deletion. Notifies the associated ad placer.
  115. *
  116. * @param indexPaths An array of NSIndexPath objects identifying the rows to delete.
  117. * @param animation A constant that indicates how the deletion is to be animated, for example, fade out or slide out from the bottom. See @c UITableViewRowAnimation for descriptions of these constants.
  118. */
  119. - (void)al_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
  120. /**
  121. * Moves the row at a specified location to a destination location. Notifies the associated ad placer.
  122. *
  123. * @param indexPath An index path identifying the row to move.
  124. * @param newIndexPath An index path identifying the row that is the destination of the row at @c indexPath. The existing row at that location slides up or down to an adjoining index position to make room for it.
  125. */
  126. - (void)al_moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;
  127. #pragma mark - Inserting, Deleting, and Moving Sections
  128. /**
  129. * Inserts one or more sections in the table view, with an option to animate the insertion. Notifies the associated ad placer.
  130. *
  131. * @param sections An index set that specifies the sections to insert in the table view. If a section already exists at the specified index location, it is moved down one index location.
  132. * @param animation A constant that indicates how the insertion is to be animated, for example, fade in or slide in from the left. See @c UITableViewRowAnimation for descriptions of these constants.
  133. */
  134. - (void)al_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
  135. /**
  136. * Deletes one or more sections in the table view, with an option to animate the deletion. Notifies the associated ad placer.
  137. *
  138. * @param sections An index set that specifies the sections to delete from the table view. If a section exists after the specified index location, it is moved up one index location.
  139. * @param animation A constant that either specifies the kind of animation to perform when deleting the section or requests no animation. See @c UITableViewRowAnimation for descriptions of the constants.
  140. */
  141. - (void)al_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
  142. /**
  143. * Moves a section to a new location in the table view.
  144. *
  145. * @param section The index of the section to move.
  146. * @param newSection The index in the table view that is the destination of the move for the section. The existing section at that location slides up or down to an adjoining index position to make room for it.
  147. */
  148. - (void)al_moveSection:(NSInteger)section toSection:(NSInteger)newSection;
  149. #pragma mark - Reloading and Updating
  150. /**
  151. * Animates multiple insert, delete, reload, and move operations as a group.
  152. *
  153. * @param updates The block that performs the relevant insert, delete, reload, or move operations. In addition to modifying the table's rows, update your table's data source to reflect your changes. This block has no return value and takes no parameters.
  154. * @param completion A completion handler block to execute when all of the operations are finished.
  155. */
  156. - (void)al_performBatchUpdates:(void (NS_NOESCAPE ^ _Nullable)(void))updates completion:(void (^ _Nullable)(BOOL finished))completion API_AVAILABLE(ios(11.0), tvos(11.0));
  157. /**
  158. * Begins a series of method calls that insert, delete, or select rows and sections of the table view.
  159. */
  160. - (void)al_beginUpdates;
  161. /**
  162. * Concludes a series of method calls that insert, delete, select, or reload rows and sections of the table view.
  163. */
  164. - (void)al_endUpdates;
  165. /**
  166. * Reloads the rows and sections of the table view.
  167. */
  168. - (void)al_reloadData;
  169. /**
  170. * Reloads the specified sections using the provided animation effect.
  171. *
  172. * @param sections An index set identifying the sections to reload.
  173. * @param animation A constant that indicates how the reloading is to be animated, for example, fade out or slide out from the bottom. See @c UITableViewRowAnimation for descriptions of these constants. The animation constant affects the direction in which both the old and the new section rows slide. For example, if the animation constant is @c UITableViewRowAnimationRight, the old rows slide out to the right and the new cells slide in from the right.
  174. */
  175. - (void)al_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
  176. /**
  177. * Reloads the specified rows using the provided animation effect.
  178. *
  179. * @param indexPaths An array of @c NSIndexPath objects identifying the rows to reload.
  180. * @param animation A constant that indicates how the reloading is to be animated, for example, fade out or slide out from the bottom. See @c UITableViewRowAnimation for descriptions of these constants. The animation constant affects the direction in which both the old and the new rows slide. For example, if the animation constant is @c UITableViewRowAnimationRight, the old rows slide out to the right and the new cells slide in from the right.
  181. */
  182. - (void)al_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
  183. #pragma mark - Scrolling the Table View
  184. /**
  185. * Scrolls through the table view until a row that an index path identifies is at a particular location on the screen.
  186. *
  187. * @param indexPath An index path that identifies a row in the table view by its row index and its section index. @c NSNotFound is a valid row index for scrolling to a section with zero rows.
  188. * @param scrollPosition A constant that identifies a relative position in the table view (top, middle, bottom) for row when scrolling concludes. See @c UITableViewScrollPosition for descriptions of valid constants.
  189. * @param animated @c YES if you want to animate the change in position; @c NO if it should be immediate.
  190. */
  191. - (void)al_scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
  192. #pragma mark - Getting the Drawing Areas for the Table
  193. /**
  194. * Returns the drawing area for a row that an index path identifies.
  195. *
  196. * @param indexPath An index path object that identifies a row by its index and its section index.
  197. *
  198. * @return A rectangle defining the area in which the table view draws the row or CGRectZero if indexPath is invalid.
  199. */
  200. - (CGRect)al_rectForRowAtIndexPath:(NSIndexPath *)indexPath;
  201. @end
  202. NS_ASSUME_NONNULL_END