BaseTableViewCell.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // BaseTableViewCell.m
  3. // zhuxun
  4. //
  5. // Created by winsoft on 16/8/24.
  6. //
  7. //
  8. #import "BaseTableViewCell.h"
  9. #define MarginX 20
  10. #define BtnWH 20
  11. @interface BaseTableViewCell()
  12. @property (nonatomic , weak, readonly) UIImageView *selectView;
  13. @end
  14. @implementation BaseTableViewCell
  15. - (void)awakeFromNib {
  16. [super awakeFromNib];
  17. // Initialization code
  18. }
  19. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
  20. {
  21. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  22. UIImageView *selectView = [[UIImageView alloc]init];
  23. [self addSubview:selectView];
  24. _selectView = selectView;;
  25. }
  26. return self;
  27. }
  28. - (void)setIndexPath:(NSIndexPath *)indexPath
  29. {
  30. _indexPath = indexPath;
  31. [self setNeedsDisplay];
  32. }
  33. - (void)drawRect:(CGRect)rect
  34. {
  35. [super drawRect:rect];
  36. long maxRowIndex = [self.tableView numberOfRowsInSection:_indexPath.section];
  37. //可能maxrowindex = 0的情况
  38. UIBezierPath *path = [[UIBezierPath alloc] init];
  39. //第一行
  40. if (self.indexPath.row == 0) {
  41. [path moveToPoint:CGPointMake(0, 0)];
  42. [path addLineToPoint:CGPointMake(self.frame.size.width, 0)];
  43. //下部分
  44. }else if (self.indexPath.row != 0)
  45. {
  46. [path moveToPoint:CGPointMake(MarginX, 0)];
  47. [path addLineToPoint:CGPointMake(self.frame.size.width, 0)];
  48. }
  49. //最后一行,需要画两行..上下.
  50. if (_indexPath.row == maxRowIndex - 1)
  51. {
  52. [path moveToPoint:CGPointMake(0, self.frame.size.height)];
  53. [path addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height)];
  54. }
  55. [path setLineWidth:0.5];
  56. [LineForDrawColor set];
  57. [path stroke];
  58. }
  59. - (void)setFrame:(CGRect)frame
  60. {
  61. CGRect orgFrame = frame;
  62. orgFrame.origin.y += self.insertY;
  63. orgFrame.size.height -= self.insertY;
  64. [super setFrame:orgFrame];
  65. }
  66. - (void)setCellSelected:(BOOL)isSelected
  67. {
  68. _cellSelected = isSelected;
  69. if (isSelected) {
  70. [self.selectView setImage:[UIImage imageNamed:@"file_selected"]];
  71. }else [self.selectView setImage:[UIImage imageNamed:@"file_unselected"]];
  72. }
  73. - (void)setAllowsMultipleSelection:(BOOL)allowsMultipleSelection
  74. {
  75. _allowsMultipleSelection = allowsMultipleSelection;
  76. [self setNeedsLayout];
  77. if (self.allowsMultipleSelection) {
  78. self.selectView.hidden = NO;
  79. }else {
  80. self.selectView.hidden = YES;
  81. }
  82. }
  83. - (void)layoutSubviews
  84. {
  85. [super layoutSubviews];
  86. if (self.allowsMultipleSelection) {
  87. self.selectView.frame = CGRectMake(5, self.frame.size.height/2.0f - BtnWH/2.0f, BtnWH, BtnWH);
  88. CGRect contentViewFram = self.contentView.frame;
  89. contentViewFram.origin.x += CGRectGetMaxX(self.selectView.frame);
  90. self.contentView.frame = contentViewFram;
  91. }else {
  92. self.selectView.frame = CGRectZero;
  93. CGRect contentViewFram = self.contentView.frame;
  94. contentViewFram.origin.x = 0;
  95. self.contentView.frame = contentViewFram;
  96. }
  97. }
  98. @end