KSSearchResultViewCell.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // KSSearchResultViewCell.m
  3. // TeacherDaya
  4. //
  5. // Created by Kyle on 2020/10/27.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "KSSearchResultViewCell.h"
  9. #import "KSSearchResultModel.h"
  10. #import "KSSearchRCLabel.h"
  11. @interface KSSearchResultViewCell ()
  12. @property (nonatomic, strong) UILabel *otherLabel;
  13. @property (nonatomic, strong) UILabel *timeLabel;
  14. @end
  15. @implementation KSSearchResultViewCell
  16. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  17. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  18. if (self) {
  19. self.backgroundColor = HexRGBAlpha(0xffffff, 0.4);
  20. self.selectionStyle = UITableViewCellSelectionStyleNone;
  21. [self loadView];
  22. }
  23. return self;
  24. }
  25. - (void)setDataModel:(KSSearchResultModel *)model {
  26. float namelLabelWidth = kScreenWidth - 20 - 48 - 9.5;
  27. if (model.time) {
  28. self.timeLabel.hidden = NO;
  29. namelLabelWidth -= 100;
  30. } else {
  31. self.timeLabel.hidden = YES;
  32. }
  33. if (!(model.otherInformation.length > 0) || !(model.name.length > 0)) {
  34. self.nameLabel.frame =
  35. CGRectMake(CGRectGetMaxX(self.headerView.frame) + 9.5, (65 - 17) / 2, namelLabelWidth, 17);
  36. self.additionalLabel.hidden = YES;
  37. self.otherLabel.hidden = YES;
  38. NSString *str = nil;
  39. if (model.otherInformation.length > 0) {
  40. str = model.otherInformation;
  41. } else {
  42. str = model.name;
  43. }
  44. [self.nameLabel attributedText:str byHighlightedText:self.searchString];
  45. } else {
  46. self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.headerView.frame) + 9.5,
  47. CGRectGetMinY(self.headerView.frame) + 3, namelLabelWidth, 17);
  48. self.additionalLabel.hidden = NO;
  49. self.otherLabel.hidden = NO;
  50. CGFloat height = CGRectGetMaxY(self.headerView.frame) - 20;
  51. CGFloat width = CGRectGetMaxX(self.headerView.frame) + 9.5;
  52. CGFloat additionalLabelWidth = kScreenWidth - 20 - 48 - 9.5;
  53. if ([model.objectName isEqualToString:@"RC:FileMsg"]) {
  54. self.otherLabel.text = @"[文件]";
  55. } else if ([model.objectName isEqualToString:@"RC:ImgTextMsg"]) {
  56. self.otherLabel.text = @"[链接]";
  57. } else {
  58. self.otherLabel.text = @"";
  59. self.otherLabel.frame = CGRectZero;
  60. CGRect rect = self.additionalLabel.frame;
  61. rect.origin.x = width;
  62. rect.size.width = additionalLabelWidth;
  63. self.additionalLabel.frame = rect;
  64. }
  65. self.otherLabel.frame = CGRectMake(width, height, 40, 16);
  66. [self.otherLabel sizeToFit];
  67. self.additionalLabel.frame = CGRectMake(width + self.otherLabel.frame.size.width, height,
  68. additionalLabelWidth - CGRectGetWidth(self.otherLabel.frame), 16);
  69. if (model.time) {
  70. self.timeLabel.text = [RCKitUtility convertConversationTime:model.time / 1000];
  71. }
  72. self.nameLabel.text = model.name;
  73. if (model.count > 1) {
  74. self.additionalLabel.text = model.otherInformation;
  75. } else {
  76. [self.additionalLabel attributedText:model.otherInformation byHighlightedText:self.searchString];
  77. }
  78. }
  79. if (!(model.portraitUri.length > 0)) {
  80. if (model.conversationType == ConversationType_GROUP) {
  81. self.headerView.image = [self imageNamed:@"default_group_portrait" ofBundle:@"RongCloud.bundle"];
  82. }
  83. else {
  84. self.headerView.image = [self imageNamed:@"default_portrait_msg" ofBundle:@"RongCloud.bundle"];
  85. }
  86. } else {
  87. if (model.conversationType == ConversationType_GROUP) {
  88. [self.headerView
  89. sd_setImageWithURL:[NSURL URLWithString:[model.portraitUri getUrlEndcodeString]]
  90. placeholderImage:[self imageNamed:@"default_group_portrait" ofBundle:@"RongCloud.bundle"]];
  91. }
  92. else {
  93. [self.headerView
  94. sd_setImageWithURL:[NSURL URLWithString:[model.portraitUri getUrlEndcodeString]]
  95. placeholderImage:[self imageNamed:@"default_portrait_msg" ofBundle:@"RongCloud.bundle"]];
  96. }
  97. }
  98. }
  99. - (void)loadView {
  100. self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (65 - 48) / 2, 48, 48)];
  101. self.headerView.layer.cornerRadius = 24;
  102. self.headerView.layer.masksToBounds = YES;
  103. [self.contentView addSubview:self.headerView];
  104. self.nameLabel = [[KSSearchRCLabel alloc] initWithFrame:CGRectZero];
  105. self.nameLabel.font = [UIFont systemFontOfSize:15.f];
  106. self.nameLabel.textColor = HexRGB(0x000000);
  107. [self.contentView addSubview:self.nameLabel];
  108. self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreen_Width - 100 - 10, (65 - 48) / 2, 100, 17)];
  109. self.timeLabel.textColor = HexRGB(0x999999);
  110. self.timeLabel.font = [UIFont systemFontOfSize:15.f];
  111. self.timeLabel.textAlignment = NSTextAlignmentRight;
  112. [self.contentView addSubview:self.timeLabel];
  113. self.otherLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  114. self.otherLabel.textColor = HexRGB(0x999999);
  115. self.otherLabel.font = [UIFont systemFontOfSize:14.f];
  116. self.additionalLabel = [[KSSearchRCLabel alloc] initWithFrame:CGRectZero];
  117. self.additionalLabel.font = [UIFont systemFontOfSize:14.f];
  118. self.additionalLabel.textColor = HexRGB(0x999999);
  119. [self.contentView addSubview:self.otherLabel];
  120. [self.contentView addSubview:self.additionalLabel];
  121. }
  122. - (UIImage *)imageNamed:(NSString *)name ofBundle:(NSString *)bundleName {
  123. UIImage *image = nil;
  124. NSString *image_name = [NSString stringWithFormat:@"%@.png", name];
  125. NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
  126. NSString *bundlePath = [resourcePath stringByAppendingPathComponent:bundleName];
  127. NSString *image_path = [bundlePath stringByAppendingPathComponent:image_name];
  128. image = [[UIImage alloc] initWithContentsOfFile:image_path];
  129. return image;
  130. }
  131. @end