// // LDClassifyLeftView.m // YouHuiProject // // Created by 小花 on 2018/4/28. // Copyright © 2018年 kuxuan. All rights reserved. // #import "LDClassifyLeftView.h" #import "LDClassifyLeftTableViewCell.h" #import "LDCategoryModel.h" static NSString *const cellIndentifier = @"classifyLeft"; @interface LDClassifyLeftView() @property (nonatomic,strong)UITableView *tableView; @property (nonatomic,strong)NSIndexPath *indexPath; @end @implementation LDClassifyLeftView { NSInteger _index; } - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor YHColorWithHex:0xF4F4F4]; [self setupUI]; } return self; } - (void)setupUI{ self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[LDClassifyLeftTableViewCell class] forCellReuseIdentifier:cellIndentifier]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.tableFooterView = [[UIView alloc]init]; self.tableView.showsVerticalScrollIndicator = NO; [self addSubview:self.tableView]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ LDClassifyLeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; if (!cell) { cell = [[LDClassifyLeftTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier]; } if ([indexPath isEqual:self.indexPath]) { cell.isSelect = YES; }else{ cell.isSelect = NO; } cell.selectionStyle = UITableViewCellSelectionStyleNone; LDCategoryModel *model = self.dataArray[indexPath.row]; cell.title = model.name; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ LDClassifyLeftTableViewCell *formerCell = [tableView cellForRowAtIndexPath:self.indexPath]; formerCell.isSelect = NO; self.indexPath = indexPath; LDClassifyLeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.isSelect = YES; if (self.selectRowBlock) { self.selectRowBlock(indexPath.row); _index = indexPath.row; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 48; } - (NSInteger)index{ return _index; } - (void)setDataArray:(NSArray *)dataArray{ _dataArray = dataArray; [self.tableView reloadData]; } - (NSIndexPath *)indexPath{ if (!_indexPath) { _indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; } return _indexPath; } @end