RAOptionViewController.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // RAOptionViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/9/12.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAOptionViewController.h"
  9. #import "RASettingOptionModel.h"
  10. #import "RAOptionCell.h"
  11. @interface RAOptionViewController () <UITableViewDelegate,UITableViewDataSource>
  12. @property (nonatomic,strong) IBOutlet UITableView *optionTable;
  13. @property (nonatomic,strong) NSArray<RASettingOption *> *options;
  14. @property (nonatomic,assign) NSInteger selectedIndex;
  15. @end
  16. @implementation RAOptionViewController
  17. + (instancetype)optionViewControllerWith:(NSArray<RASettingOption *> *)options selectedIndex:(NSInteger)index {
  18. RAOptionViewController *optionVC = [self viewControllerFromStoryboard];
  19. optionVC.options = options;
  20. optionVC.selectedIndex = index;
  21. return optionVC;
  22. }
  23. + (instancetype)viewControllerFromStoryboard {
  24. RAOptionViewController *optionVC = [[UIStoryboard storyboardWithName:@"setting" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  25. return optionVC;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. [self configTable];
  31. }
  32. - (void)didReceiveMemoryWarning {
  33. [super didReceiveMemoryWarning];
  34. // Dispose of any resources that can be recreated.
  35. }
  36. #pragma mark - config
  37. - (void)configTable {
  38. if (@available(iOS 11,*)) {
  39. self.optionTable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  40. } else {
  41. self.automaticallyAdjustsScrollViewInsets = NO;
  42. }
  43. self.optionTable.tableFooterView = [UIView new];
  44. self.optionTable.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
  45. }
  46. #pragma mark - Table
  47. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  48. return 1;
  49. }
  50. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  51. return self.options.count;
  52. }
  53. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  54. RAOptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RAOptionCell" forIndexPath:indexPath];
  55. RASettingOption *option = [self.options objectAtIndex:indexPath.row];
  56. [cell setTitle:option.title];
  57. if (self.selectedIndex == indexPath.row) {
  58. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  59. } else {
  60. cell.accessoryType = UITableViewCellAccessoryNone;
  61. }
  62. return cell;
  63. }
  64. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. return 50.0f;
  66. }
  67. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  68. return 25.0f;
  69. }
  70. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  71. UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 25.0f)];
  72. // header.backgroundColor = ApexDriverGrayColor;
  73. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, CGRectGetWidth(tableView.bounds) - 20, 21)];
  74. label.font = [UIFont systemFontOfSize:14.0f];
  75. label.textColor = [UIColor grayColor];
  76. // label.textColor = ApexDriverWhiteColor;
  77. [header addSubview:label];
  78. label.text = self.optionTitle;
  79. return header;
  80. }
  81. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  82. self.selectedIndex = indexPath.row;
  83. if (self.selectBlk) {
  84. self.selectBlk(self.selectedIndex);
  85. }
  86. [tableView reloadData];
  87. }
  88. @end