StaticContentTableViewManager.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2017 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. #pragma mark - UITableViewDelegate
  46. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  47. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  48. if (cellData.customCell) {
  49. return cellData.customCell.frame.size.height;
  50. }
  51. return 44;
  52. }
  53. - (UITableViewCell *)tableView:(UITableView *)tableView
  54. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  55. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  56. UITableViewCell *cell = cellData.customCell;
  57. if (cell) {
  58. return cell;
  59. }
  60. if (cellData.value.length) {
  61. cell = [tableView dequeueReusableCellWithIdentifier:kValueCellReuseIdentitfier];
  62. if (!cell) {
  63. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
  64. reuseIdentifier:kValueCellReuseIdentitfier];
  65. cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
  66. cell.detailTextLabel.minimumScaleFactor = 0.5;
  67. }
  68. cell.detailTextLabel.text = cellData.value;
  69. } else {
  70. // kCellReuseIdentitfier has already been registered.
  71. cell = [tableView dequeueReusableCellWithIdentifier:kCellReuseIdentitfier
  72. forIndexPath:indexPath];
  73. }
  74. cell.textLabel.text = cellData.title;
  75. cell.accessibilityIdentifier = cellData.accessibilityIdentifier;
  76. return cell;
  77. }
  78. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  79. StaticContentTableViewCell *cellData = _contents.sections[indexPath.section].cells[indexPath.row];
  80. BOOL hasAssociatedAction = cellData.action != nil;
  81. if (hasAssociatedAction) {
  82. cellData.action();
  83. }
  84. [tableView deselectRowAtIndexPath:indexPath animated:hasAssociatedAction];
  85. }
  86. @end
  87. #pragma mark -
  88. @implementation StaticContentTableViewContent
  89. + (nullable instancetype)contentWithSections:
  90. (nullable NSArray<StaticContentTableViewSection *> *)sections {
  91. return [[self alloc] initWithSections:sections];
  92. }
  93. - (nullable instancetype)initWithSections:
  94. (nullable NSArray<StaticContentTableViewSection *> *)sections {
  95. self = [super init];
  96. if (self) {
  97. _sections = [sections copy];
  98. }
  99. return self;
  100. }
  101. @end
  102. #pragma mark -
  103. @implementation StaticContentTableViewSection
  104. + (nullable instancetype)sectionWithTitle:(nullable NSString *)title
  105. cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  106. return [[self alloc] initWithTitle:title cells:cells];
  107. }
  108. - (nullable instancetype)initWithTitle:(nullable NSString *)title
  109. cells:(nullable NSArray<StaticContentTableViewCell *> *)cells {
  110. self = [super init];
  111. if (self) {
  112. _title = [title copy];
  113. _cells = [cells copy];
  114. }
  115. return self;
  116. }
  117. @end
  118. #pragma mark -
  119. @implementation StaticContentTableViewCell
  120. + (nullable instancetype)cellWithTitle:(nullable NSString *)title {
  121. return [[self alloc] initWithCustomCell:nil
  122. title:title
  123. value:nil
  124. action:nil
  125. accessibilityID:nil];
  126. }
  127. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  128. value:(nullable NSString *)value {
  129. return [[self alloc] initWithCustomCell:nil
  130. title:title
  131. value:value
  132. action:nil
  133. accessibilityID:nil];
  134. }
  135. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  136. action:(nullable StaticContentTableViewCellAction)action {
  137. return [[self alloc] initWithCustomCell:nil
  138. title:title
  139. value:nil
  140. action:action
  141. accessibilityID:nil];
  142. }
  143. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  144. value:(nullable NSString *)value
  145. action:(nullable StaticContentTableViewCellAction)action {
  146. return [[self alloc] initWithCustomCell:nil
  147. title:title
  148. value:value
  149. action:action
  150. accessibilityID:nil];
  151. }
  152. + (nullable instancetype)cellWithTitle:(nullable NSString *)title
  153. value:(nullable NSString *)value
  154. action:(nullable StaticContentTableViewCellAction)action
  155. accessibilityID:(nullable NSString *)accessibilityID {
  156. return [[self alloc] initWithCustomCell:nil
  157. title:title
  158. value:value
  159. action:action
  160. accessibilityID:accessibilityID];
  161. }
  162. + (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell {
  163. return [[self alloc] initWithCustomCell:customCell
  164. title:nil
  165. value:nil
  166. action:nil
  167. accessibilityID:nil];
  168. }
  169. + (nullable instancetype)cellWithCustomCell:(nullable UITableViewCell *)customCell
  170. action:(nullable StaticContentTableViewCellAction)action {
  171. return [[self alloc] initWithCustomCell:customCell
  172. title:nil
  173. value:nil action:action
  174. accessibilityID:nil];
  175. }
  176. - (nullable instancetype)initWithCustomCell:(nullable UITableViewCell *)customCell
  177. title:(nullable NSString *)title
  178. value:(nullable NSString *)value
  179. action:(nullable StaticContentTableViewCellAction)action
  180. accessibilityID:(nullable NSString *)accessibilityID {
  181. self = [super init];
  182. if (self) {
  183. _customCell = customCell;
  184. _title = [title copy];
  185. _value = [value copy];
  186. _action = action;
  187. if (accessibilityID) {
  188. _accessibilityIdentifier = [accessibilityID copy];
  189. self.isAccessibilityElement = YES;
  190. }
  191. }
  192. return self;
  193. }
  194. @end