SPSBHorizontalListTableView.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // SPSBHorizontalListTableView.m
  3. // 我的社保
  4. //
  5. // Created by jiaxian_he on 2021/5/29.
  6. //
  7. #import "SPSBHorizontalListTableView.h"
  8. #import "SPSBUIGeneralHeader.h"
  9. @interface SPSBHorizontalListTableView ()<UITableViewDelegate, UITableViewDataSource> {
  10. NSDictionary *_data;
  11. bool _isContentRightAlignment;
  12. bool _isLittleLayout;
  13. }
  14. @end
  15. @implementation SPSBHorizontalListTableView
  16. - (instancetype)initWithData:(nullable NSDictionary *)data isContentRightAlignment:(bool)isContentRightAlignment isLittleLayout:(bool)isLittleLayout {
  17. self = [super initWithFrame:CGRectZero style:UITableViewStylePlain];
  18. if (!self) return nil;
  19. _isContentRightAlignment = isContentRightAlignment;
  20. _isLittleLayout = isLittleLayout;
  21. _data = data;
  22. [self setupTableView];
  23. return self;
  24. }
  25. - (CGFloat)spsb_height {
  26. if (!_data || ((NSArray *)_data[@"title"]).count == 0) return 0;
  27. return ((NSArray *)_data[@"title"]).count * 25 + 20;
  28. }
  29. - (void)reloadData:(NSDictionary *)data {
  30. _data = data;
  31. [self reloadData];
  32. }
  33. #pragma mark - Delegate & DataSource
  34. static NSString * const reuseIdentifier = @"SPSBHorizontalListTableViewCell";
  35. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  36. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  37. if (!cell) {
  38. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
  39. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  40. UILabel *titleLabel = [UILabel convenienceWithFont:spsb_font(_isLittleLayout ? 13 : 14) text:nil textColor:spsb_999999_color()];
  41. titleLabel.tag = 3000;
  42. [cell.contentView addSubview:titleLabel];
  43. [titleLabel makeConstraints:^(JXHConstraintMaker *make) {
  44. make.leading.equalTo(15);
  45. make.centerY.equalTo(0);
  46. make.xContentCompressionResistance(UILayoutPriorityRequired);
  47. }];
  48. UILabel *contentLabel = [UILabel convenienceWithFont:spsb_font(_isLittleLayout ? 13 : 14) text:@"" textColor:_isLittleLayout ? spsb_999999_color() : spsb_666666_color() textAlignment:_isContentRightAlignment ? NSTextAlignmentRight : NSTextAlignmentLeft];
  49. contentLabel.tag = 3001;
  50. [cell.contentView addSubview:contentLabel];
  51. if (_isContentRightAlignment) {
  52. [contentLabel makeConstraints:^(JXHConstraintMaker *make) {
  53. make.trailing.equalTo(-15);
  54. make.centerY.equalTo(0);
  55. make.leading.greaterThanOrEqualTo(titleLabel.trailing).offset(40);
  56. }];
  57. } else {
  58. [contentLabel makeConstraints:^(JXHConstraintMaker *make) {
  59. make.leading.equalTo(titleLabel.trailing).offset(35);
  60. make.centerY.equalTo(0);
  61. make.trailing.lessThanOrEqualTo(-15);
  62. }];
  63. }
  64. }
  65. UILabel *titleLabel = [cell.contentView viewWithTag:3000];
  66. UILabel *contentLabel = [cell.contentView viewWithTag:3001];
  67. if (((NSArray *)_data[@"title"]).count > indexPath.row) {
  68. titleLabel.text = _data[@"title"][indexPath.row];
  69. contentLabel.text = _data[@"content"][indexPath.row];
  70. }
  71. return cell;
  72. }
  73. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  74. if (!_data) return 0;
  75. return ((NSArray *)_data[@"title"]).count;
  76. }
  77. #pragma mark - UI
  78. - (void)setupTableView {
  79. self.scrollEnabled = false;
  80. self.tableHeaderView = [[UIView alloc] initWithFrame:(CGRect){0, 0, jxh_screenWidth(), 10}];
  81. self.tableFooterView = [[UIView alloc] initWithFrame:(CGRect){0, 0, jxh_screenWidth(), 10}];
  82. self.rowHeight = 25.f;
  83. self.separatorStyle = UITableViewCellSeparatorStyleNone;
  84. self.showsHorizontalScrollIndicator = false;
  85. self.showsVerticalScrollIndicator = false;
  86. self.delegate = self;
  87. self.dataSource = self;
  88. }
  89. @end