猎豆优选

LDClassifyLeftView.m 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // LDClassifyLeftView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/4/28.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "LDClassifyLeftView.h"
  9. #import "LDClassifyLeftTableViewCell.h"
  10. #import "LDCategoryModel.h"
  11. static NSString *const cellIndentifier = @"classifyLeft";
  12. @interface LDClassifyLeftView()<UITableViewDelegate,UITableViewDataSource>
  13. @property (nonatomic,strong)UITableView *tableView;
  14. @property (nonatomic,strong)NSIndexPath *indexPath;
  15. @end
  16. @implementation LDClassifyLeftView
  17. {
  18. NSInteger _index;
  19. }
  20. - (instancetype)initWithFrame:(CGRect)frame{
  21. if (self = [super initWithFrame:frame]) {
  22. self.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4];
  23. [self setupUI];
  24. }
  25. return self;
  26. }
  27. - (void)setupUI{
  28. self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
  29. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  30. [self.tableView registerClass:[LDClassifyLeftTableViewCell class] forCellReuseIdentifier:cellIndentifier];
  31. self.tableView.delegate = self;
  32. self.tableView.dataSource = self;
  33. self.tableView.tableFooterView = [[UIView alloc]init];
  34. self.tableView.showsVerticalScrollIndicator = NO;
  35. [self addSubview:self.tableView];
  36. }
  37. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  38. return self.dataArray.count;
  39. }
  40. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  41. LDClassifyLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
  42. if (!cell) {
  43. cell = [[LDClassifyLeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
  44. }
  45. if ([indexPath isEqual:self.indexPath]) {
  46. cell.isSelect = YES;
  47. }else{
  48. cell.isSelect = NO;
  49. }
  50. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  51. LDCategoryModel *model = self.dataArray[indexPath.row];
  52. cell.title = model.name;
  53. return cell;
  54. }
  55. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  56. LDClassifyLeftTableViewCell *formerCell = [tableView cellForRowAtIndexPath:self.indexPath];
  57. formerCell.isSelect = NO;
  58. self.indexPath = indexPath;
  59. LDClassifyLeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  60. cell.isSelect = YES;
  61. if (self.selectRowBlock) {
  62. self.selectRowBlock(indexPath.row);
  63. _index = indexPath.row;
  64. }
  65. }
  66. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  67. return 48;
  68. }
  69. - (NSInteger)index{
  70. return _index;
  71. }
  72. - (void)setDataArray:(NSArray *)dataArray{
  73. _dataArray = dataArray;
  74. [self.tableView reloadData];
  75. }
  76. - (NSIndexPath *)indexPath{
  77. if (!_indexPath) {
  78. _indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  79. }
  80. return _indexPath;
  81. }
  82. @end