OneSectionCell.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // OneSectionCell.m
  3. // BuguLive
  4. // Created by fanwe2014 on 16/7/4.
  5. // Copyright © 2016年 xfg. All rights reserved.
  6. #import "OneSectionCell.h"
  7. @implementation OneSectionCell
  8. - (instancetype)initWithFrame:(CGRect)frame
  9. {
  10. self = [super initWithFrame:frame];
  11. if (self)
  12. {
  13. }
  14. return self;
  15. }
  16. - (CGFloat)creatCellWithArray:(NSMutableArray *)array
  17. {
  18. for (UIView *subView in self.contentView.subviews)
  19. {
  20. [subView removeFromSuperview];
  21. }
  22. @autoreleasepool
  23. {
  24. if (array.count%2 == 0)//当主题列表是偶数个数时
  25. {
  26. int section = (int)array.count/2;//有多少段
  27. for (int i = 0; i < section; i ++)
  28. {
  29. for (int j = 0; j < 2; j ++)
  30. {
  31. cuserModel *model = array[j+2*i];
  32. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  33. button.frame = CGRectMake(kScreenW*j/2+0.5*j+10, 41*i,(kScreenW-40)/2-0.5, 40);
  34. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  35. [button setTitleColor:myTextColorLine6 forState:0];
  36. button.tag = j+ 2*i;
  37. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  38. if (i == section-1 && j == 1)
  39. {
  40. [button setTitle:[NSString stringWithFormat:@"%@",model.title] forState:0];
  41. }else
  42. {
  43. [button setTitle:model.title forState:0];
  44. }
  45. button.titleLabel.font = [UIFont systemFontOfSize:15];
  46. [self.contentView addSubview:button];
  47. //横分割线
  48. if (i < section + 1 && j == 1 && i > 0)
  49. {
  50. UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(10, 40*i+(i-1), kScreenW-20, 1)];
  51. lineView2.backgroundColor = myTextColorLine3;
  52. lineView2.alpha = 0.3;
  53. [self.contentView addSubview:lineView2];
  54. }
  55. }
  56. }
  57. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/2-0.5, 10, 1, 40*section-20)];
  58. lineView.alpha = 0.3;
  59. lineView.backgroundColor = myTextColorLine3;
  60. [self.contentView addSubview:lineView];
  61. return 41*section;//返回的高度
  62. }else//当主题列表是奇数个数时
  63. {
  64. int section = (int)array.count/2+1;//有多少段
  65. for (int i = 0; i < section; i ++)
  66. {
  67. for (int j = 0; j < 2; j ++)
  68. {
  69. if (i == section-1 && j == 1)
  70. {
  71. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  72. button.frame = CGRectMake(kScreenW*j/2+0.5*j+10, 41*i,(kScreenW-40)/2-0.5, 40);
  73. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  74. [button setTitleColor:myTextColorLine6 forState:0];
  75. [self.contentView addSubview:button];
  76. }else
  77. {
  78. cuserModel *model = array[j+2*i];
  79. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  80. button.frame = CGRectMake(kScreenW*j/2+0.5*j+10, 41*i,(kScreenW-40)/2-0.5, 40);
  81. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  82. [button setTitleColor:myTextColorLine6 forState:0];
  83. button.tag = j+ 2*i;
  84. [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
  85. if (i == section-1 && j == 0)
  86. {
  87. [button setTitle:[NSString stringWithFormat:@"%@",model.title] forState:0];
  88. }else
  89. {
  90. [button setTitle:model.title forState:0];
  91. }
  92. button.titleLabel.font = [UIFont systemFontOfSize:15];
  93. [self.contentView addSubview:button];
  94. }
  95. //横分割线
  96. if (i < section + 1 && j == 1 && i > 0)
  97. {
  98. UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(10, 40*i+(i-1), kScreenW-20, 1)];
  99. lineView2.backgroundColor = myTextColorLine3;
  100. lineView2.alpha = 0.3;
  101. [self.contentView addSubview:lineView2];
  102. }
  103. //最后一个话题的箭头图标
  104. if (i == section-1 && j == 0)
  105. {
  106. UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-10, 10, 10, 20)];
  107. imgView.image = [UIImage imageNamed:@""];
  108. [self.contentView addSubview:imgView];
  109. }
  110. }
  111. }
  112. UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kScreenW/2-0.5, 10, 1, 40*section-20)];
  113. lineView.alpha = 0.3;
  114. lineView.backgroundColor = myTextColorLine3;
  115. [self.contentView addSubview:lineView];
  116. return 41*section;//返回的高度
  117. }
  118. }
  119. }
  120. //点击事件,block传值
  121. - (void)buttonClick:(UIButton *)button
  122. {
  123. int index = (int) button.tag;
  124. if (self.block)
  125. {
  126. self.block(index);
  127. }
  128. }
  129. @end