// // BaseTableViewCell.m // zhuxun // // Created by winsoft on 16/8/24. // // #import "BaseTableViewCell.h" #define MarginX 20 #define BtnWH 20 @interface BaseTableViewCell() @property (nonatomic , weak, readonly) UIImageView *selectView; @end @implementation BaseTableViewCell - (void)awakeFromNib { [super awakeFromNib]; // Initialization code } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { UIImageView *selectView = [[UIImageView alloc]init]; [self addSubview:selectView]; _selectView = selectView;; } return self; } - (void)setIndexPath:(NSIndexPath *)indexPath { _indexPath = indexPath; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { [super drawRect:rect]; long maxRowIndex = [self.tableView numberOfRowsInSection:_indexPath.section]; //可能maxrowindex = 0的情况 UIBezierPath *path = [[UIBezierPath alloc] init]; //第一行 if (self.indexPath.row == 0) { [path moveToPoint:CGPointMake(0, 0)]; [path addLineToPoint:CGPointMake(self.frame.size.width, 0)]; //下部分 }else if (self.indexPath.row != 0) { [path moveToPoint:CGPointMake(MarginX, 0)]; [path addLineToPoint:CGPointMake(self.frame.size.width, 0)]; } //最后一行,需要画两行..上下. if (_indexPath.row == maxRowIndex - 1) { [path moveToPoint:CGPointMake(0, self.frame.size.height)]; [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)]; } [path setLineWidth:0.5]; [LineForDrawColor set]; [path stroke]; } - (void)setFrame:(CGRect)frame { CGRect orgFrame = frame; orgFrame.origin.y += self.insertY; orgFrame.size.height -= self.insertY; [super setFrame:orgFrame]; } - (void)setCellSelected:(BOOL)isSelected { _cellSelected = isSelected; if (isSelected) { [self.selectView setImage:[UIImage imageNamed:@"file_selected"]]; }else [self.selectView setImage:[UIImage imageNamed:@"file_unselected"]]; } - (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection { _allowsMultipleSelection = allowsMultipleSelection; [self setNeedsLayout]; if (self.allowsMultipleSelection) { self.selectView.hidden = NO; }else { self.selectView.hidden = YES; } } - (void)layoutSubviews { [super layoutSubviews]; if (self.allowsMultipleSelection) { self.selectView.frame = CGRectMake(5, self.frame.size.height/2.0f - BtnWH/2.0f, BtnWH, BtnWH); CGRect contentViewFram = self.contentView.frame; contentViewFram.origin.x += CGRectGetMaxX(self.selectView.frame); self.contentView.frame = contentViewFram; }else { self.selectView.frame = CGRectZero; CGRect contentViewFram = self.contentView.frame; contentViewFram.origin.x = 0; self.contentView.frame = contentViewFram; } } @end