DataPickerViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2021 Google LLC
  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 "DataPickerState.h"
  17. #import "DataPickerViewController.h"
  18. @implementation DataPickerViewController
  19. - (id)initWithNibName:(NSString *)nibNameOrNil
  20. bundle:(NSBundle *)nibBundleOrNil
  21. dataState:(DataPickerState *)dataState {
  22. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  23. if (self) {
  24. _dataState = dataState;
  25. }
  26. return self;
  27. }
  28. #pragma mark - Table view data source
  29. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  30. return 1;
  31. }
  32. - (NSInteger)tableView:(UITableView *)tableView
  33. numberOfRowsInSection:(NSInteger)section {
  34. return [self.dataState.cellLabels count];
  35. }
  36. - (UITableViewCell *)tableView:(UITableView *)tableView
  37. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  38. static NSString * const kCellIdentifier = @"Cell";
  39. UITableViewCell *cell =
  40. [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
  41. if (cell == nil) {
  42. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  43. reuseIdentifier:kCellIdentifier];
  44. }
  45. NSDictionary *cellLabelDict = self.dataState.cellLabels[indexPath.row];
  46. NSString *cellLabelText = cellLabelDict[kLabelKey];
  47. if ([cellLabelDict objectForKey:kShortLabelKey]) {
  48. cellLabelText = [cellLabelDict objectForKey:kShortLabelKey];
  49. }
  50. cell.textLabel.text = cellLabelText;
  51. // If the cell is selected, mark it as checked
  52. if ([self.dataState.selectedCells containsObject:cellLabelDict[kLabelKey]]) {
  53. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  54. } else {
  55. cell.accessoryType = UITableViewCellAccessoryNone;
  56. }
  57. return cell;
  58. }
  59. #pragma mark - Table view delegate
  60. - (void)tableView:(UITableView *)tableView
  61. didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  62. UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
  63. NSDictionary *selectedCellDict = _dataState.cellLabels[indexPath.row];
  64. NSString *label = selectedCellDict[kLabelKey];
  65. if (self.dataState.multipleSelectEnabled) {
  66. // If multiple selections are allowed, then toggle the state
  67. // of the selected cell
  68. if ([self.dataState.selectedCells containsObject:label]) {
  69. [self.dataState.selectedCells removeObject:label];
  70. selectedCell.accessoryType = UITableViewCellAccessoryNone;
  71. } else {
  72. [self.dataState.selectedCells addObject:label];
  73. selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
  74. }
  75. } else {
  76. // Set all cells to unchecked except for the one that was just selected
  77. [self.dataState.selectedCells removeAllObjects];
  78. [self.dataState.selectedCells addObject:label];
  79. for (NSIndexPath *curPath in [self.tableView indexPathsForVisibleRows]) {
  80. UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:curPath];
  81. if (curPath.row == indexPath.row) {
  82. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  83. } else {
  84. cell.accessoryType = UITableViewCellAccessoryNone;
  85. }
  86. }
  87. }
  88. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  89. }
  90. @end