StaticContentTableViewManager.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "StaticContentTableViewManager.h"
  17. /** @var kCellReuseIdentitfier
  18. @brief The reuse identifier for default style table view cell.
  19. */
  20. static NSString *const kCellReuseIdentitfier = @"reuseIdentifier";
  21. /** @var kCellReuseIdentitfier
  22. @brief The reuse identifier for value style table view cell.
  23. */
  24. static NSString *const kValueCellReuseIdentitfier = @"reuseValueIdentifier";
  25. #pragma mark -
  26. @implementation StaticContentTableViewManager
  27. - (void)setContents:(StaticContentTableViewContent *)contents {
  28. _contents = contents;
  29. [self.tableView reloadData];
  30. }
  31. - (void)setTableView:(UITableView *)tableView {
  32. _tableView = tableView;
  33. [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellReuseIdentitfier];
  34. }
  35. #pragma mark - UITableViewDataSource
  36. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  37. return _contents.sections.count;
  38. }
  39. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  40. return _contents.sections[section].cells.count;
  41. }
  42. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  43. return _contents.sections[section].title;
  44. }
  45. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
  46. return index;
  47. }
  48. - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  49. NSMutableArray<NSString *> *sectionTitles = [NSMutableArray array];
  50. for (StaticContentTableViewSection *section in _contents.sections) {
  51. [sectionTitles addObject:[section.title substringToIndex:3]];
  52. }
  53. return sectionTitles;
  54. }
  55. #pragma mark - UITableViewDelegate
  56. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  57. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  58. if (cellData.customCell) {
  59. return cellData.customCell.frame.size.height;
  60. }
  61. return 44;
  62. }
  63. - (UITableViewCell *)tableView:(UITableView *)tableView
  64. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  66. UITableViewCell *cell = cellData.customCell;
  67. if (cell) {
  68. return cell;
  69. }
  70. if (cellData.value.length) {
  71. cell = [tableView dequeueReusableCellWithIdentifier:kValueCellReuseIdentitfier];
  72. if (!cell) {
  73. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  74. reuseIdentifier:kValueCellReuseIdentitfier];
  75. cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
  76. cell.detailTextLabel.minimumScaleFactor = 0.5;
  77. }
  78. cell.detailTextLabel.text = cellData.value;
  79. } else {
  80. // kCellReuseIdentitfier has already been registered.
  81. cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentitfier
  82. forIndexPath:indexPath];
  83. }
  84. cell.textLabel.text = cellData.title;
  85. cell.accessibilityIdentifier = cellData.accessibilityIdentifier;
  86. return cell;
  87. }
  88. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  89. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  90. BOOL hasAssociatedAction = cellData.action != nil;
  91. if (hasAssociatedAction) {
  92. cellData.action();
  93. }
  94. [tableView deselectRowAtIndexPath:indexPath animated:hasAssociatedAction];
  95. }
  96. @end
  97. #pragma mark -
  98. @implementation StaticContentTableViewContent
  99. + (nullable instancetype)contentWithSections:
  100. (nullable NSArray<StaticContentTableViewSection *> *)sections {
  101. return [[self alloc] initWithSections:sections];
  102. }
  103. - (nullable instancetype)initWithSections:
  104. (nullable NSArray<StaticContentTableViewSection *> *)sections {
  105. self = [super init];
  106. if (self) {
  107. _sections = [sections copy];
  108. }
  109. return self;
  110. }
  111. @end
  112. #pragma mark -
  113. @implementation StaticContentTableViewSection
  114. + (nullable instancetype)sectionWithTitle:(nullable NSString *)title
  115. cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  116. return [[self alloc] initWithTitle:title cells:cells];
  117. }
  118. - (nullable instancetype)initWithTitle:(nullable NSString *)title
  119. cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  120. self = [super init];
  121. if (self) {
  122. _title = [title copy];
  123. _cells = [cells copy];
  124. }
  125. return self;
  126. }
  127. @end
  128. #pragma mark -
  129. @implementation StaticContentTableViewCell
  130. + (nullable instancetype)cellWithTitle:(nullable NSString *)title {
  131. return [[self alloc] initWithCustomCell:nil
  132. title:title
  133. value:nil
  134. action:nil
  135. accessibilityID:nil];
  136. }
  137. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  138. value:(nullable NSString *)value {
  139. return [[self alloc] initWithCustomCell:nil
  140. title:title
  141. value:value
  142. action:nil
  143. accessibilityID:nil];
  144. }
  145. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  146. action:(nullable StaticContentTableViewCellAction)action {
  147. return [[self alloc] initWithCustomCell:nil
  148. title:title
  149. value:nil
  150. action:action
  151. accessibilityID:nil];
  152. }
  153. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  154. value:(nullable NSString *)value
  155. action:(nullable StaticContentTableViewCellAction)action {
  156. return [[self alloc] initWithCustomCell:nil
  157. title:title
  158. value:value
  159. action:action
  160. accessibilityID:nil];
  161. }
  162. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  163. value:(nullable NSString *)value
  164. action:(nullable StaticContentTableViewCellAction)action
  165. accessibilityID:(nullable NSString *)accessibilityID {
  166. return [[self alloc] initWithCustomCell:nil
  167. title:title
  168. value:value
  169. action:action
  170. accessibilityID:accessibilityID];
  171. }
  172. + (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell {
  173. return [[self alloc] initWithCustomCell:customCell
  174. title:nil
  175. value:nil
  176. action:nil
  177. accessibilityID:nil];
  178. }
  179. + (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell
  180. action:(nullable StaticContentTableViewCellAction)action {
  181. return [[self alloc] initWithCustomCell:customCell
  182. title:nil
  183. value:nil action:action
  184. accessibilityID:nil];
  185. }
  186. - (nullable instancetype)initWithCustomCell:(nullable UITableViewCell *)customCell
  187. title:(nullable NSString *)title
  188. value:(nullable NSString *)value
  189. action:(nullable StaticContentTableViewCellAction)action
  190. accessibilityID:(nullable NSString *)accessibilityID {
  191. self = [super init];
  192. if (self) {
  193. _customCell = customCell;
  194. _title = [title copy];
  195. _value = [value copy];
  196. _action = action;
  197. if (accessibilityID) {
  198. _accessibilityIdentifier = [accessibilityID copy];
  199. self.isAccessibilityElement = YES;
  200. }
  201. }
  202. return self;
  203. }
  204. @end