FLEXDBQueryRowCell.m 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // FLEXDBQueryRowCell.m
  3. // FLEX
  4. //
  5. // Created by Peng Tao on 15/11/24.
  6. // Copyright © 2015年 f. All rights reserved.
  7. //
  8. #import "FLEXDBQueryRowCell.h"
  9. #import "FLEXMultiColumnTableView.h"
  10. #import "NSArray+FLEX.h"
  11. #import "UIFont+FLEX.h"
  12. #import "FLEXColor.h"
  13. NSString * const kFLEXDBQueryRowCellReuse = @"kFLEXDBQueryRowCellReuse";
  14. @interface FLEXDBQueryRowCell ()
  15. @property (nonatomic) NSInteger columnCount;
  16. @property (nonatomic) NSArray<UILabel *> *labels;
  17. @end
  18. @implementation FLEXDBQueryRowCell
  19. - (void)setData:(NSArray *)data {
  20. _data = data;
  21. self.columnCount = data.count;
  22. [self.labels flex_forEach:^(UILabel *label, NSUInteger idx) {
  23. id content = self.data[idx];
  24. if ([content isKindOfClass:[NSString class]]) {
  25. label.text = content;
  26. } else if (content == NSNull.null) {
  27. label.text = @"<null>";
  28. label.textColor = FLEXColor.deemphasizedTextColor;
  29. } else {
  30. label.text = [content description];
  31. }
  32. }];
  33. }
  34. - (void)setColumnCount:(NSInteger)columnCount {
  35. if (columnCount != _columnCount) {
  36. _columnCount = columnCount;
  37. // Remove existing labels
  38. for (UILabel *l in self.labels) {
  39. [l removeFromSuperview];
  40. }
  41. // Create new labels
  42. self.labels = [NSArray flex_forEachUpTo:columnCount map:^id(NSUInteger i) {
  43. UILabel *label = [UILabel new];
  44. label.font = UIFont.flex_defaultTableCellFont;
  45. label.textAlignment = NSTextAlignmentLeft;
  46. [self.contentView addSubview:label];
  47. return label;
  48. }];
  49. }
  50. }
  51. - (void)layoutSubviews {
  52. [super layoutSubviews];
  53. CGFloat width = self.contentView.frame.size.width / self.labels.count;
  54. CGFloat height = self.contentView.frame.size.height;
  55. [self.labels flex_forEach:^(UILabel *label, NSUInteger i) {
  56. label.frame = CGRectMake(width * i + 5, 0, (width - 10), height);
  57. }];
  58. }
  59. @end