JXMoreSelectVC.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // JXMoreSelectVC.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2019/4/16.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXMoreSelectVC.h"
  9. @interface JXMoreSelectVC () <UITableViewDelegate, UITableViewDataSource>
  10. @property (nonatomic, strong) NSString *titleStr;
  11. @property (nonatomic, strong) NSArray *dataArray;
  12. @property (nonatomic, strong) NSMutableArray *selList;
  13. @property (nonatomic, strong) NSMutableArray *indexArr;
  14. @property (nonatomic, strong) UIView *baseView;
  15. @property (nonatomic, strong) UITableView *tableView;
  16. @end
  17. @implementation JXMoreSelectVC
  18. - (instancetype)initWithTitle:(NSString *)title dataArray:(NSArray *)dataArray {
  19. if (self = [super init]) {
  20. self.titleStr = title;
  21. self.dataArray = dataArray;
  22. }
  23. return self;
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
  28. self.baseView = [[UIView alloc] initWithFrame:self.view.bounds];
  29. self.baseView.backgroundColor = [UIColor clearColor];
  30. [self.view addSubview:self.baseView];
  31. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView)];
  32. // [self.baseView addGestureRecognizer:tap];
  33. CGFloat height = self.dataArray.count *44+44*2;
  34. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(40, (JX_SCREEN_HEIGHT-height)/2, JX_SCREEN_WIDTH-40*2, height)];
  35. self.tableView.delegate = self;
  36. self.tableView.dataSource = self;
  37. [self.baseView addSubview:self.tableView];
  38. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  39. self.tableView.scrollEnabled = NO;
  40. self.tableView.showsHorizontalScrollIndicator = NO;
  41. self.tableView.showsVerticalScrollIndicator = NO;
  42. self.tableView.layer.cornerRadius = 7.f;
  43. self.tableView.layer.masksToBounds = YES;
  44. self.indexArr = [NSMutableArray arrayWithArray:[self.indexStr componentsSeparatedByString:@","]];
  45. [self.indexArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  46. if ([obj intValue] == 0) {
  47. [self.indexArr removeObject:obj];
  48. }
  49. }];
  50. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 44)];
  51. title.text = self.titleStr;
  52. title.textAlignment = NSTextAlignmentCenter;
  53. self.tableView.tableHeaderView = title;
  54. UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 44)];
  55. [sureBtn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  56. [sureBtn setTitleColor:HEXCOLOR(0x383893) forState:UIControlStateNormal];
  57. [sureBtn addTarget:self action:@selector(clickSureBtn) forControlEvents:UIControlEventTouchUpInside];
  58. self.tableView.tableFooterView = sureBtn;
  59. }
  60. #pragma mark - Table view data source
  61. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  62. return 1;
  63. }
  64. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  65. return self.dataArray.count;
  66. }
  67. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  68. return 44;
  69. }
  70. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  71. static NSString *identifier = @"moreSelectView";
  72. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  73. if (!cell) {
  74. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
  75. }
  76. cell.textLabel.text = self.dataArray[indexPath.row];
  77. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  78. for (NSNumber *index in self.indexArr) {
  79. if ([index integerValue] == indexPath.row+1) {
  80. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  81. }
  82. }
  83. return cell;
  84. }
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  86. UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  87. if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
  88. cell.accessoryType = UITableViewCellAccessoryNone;
  89. [self.indexArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  90. if ([obj intValue] == indexPath.row+1) {
  91. [self.indexArr removeObject:obj];
  92. }
  93. }];
  94. }else {
  95. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  96. [self.indexArr addObject:@(indexPath.row+1)];
  97. }
  98. NSLog(@"self.indexArr = %@",self.indexArr);
  99. }
  100. - (void)clickSureBtn {
  101. NSString *str = [self.indexArr componentsJoinedByString:@","];
  102. if (self.delegate &&[self.delegate respondsToSelector:@selector(didSureBtn:indexStr:)]) {
  103. [self.delegate didSureBtn:self indexStr:str];
  104. [self hideView];
  105. }
  106. }
  107. - (void)hideView {
  108. if (self) {
  109. [self.view removeFromSuperview];
  110. }
  111. }
  112. @end