TradeTableViewCell.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // TradeTableViewCell.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/7/1.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "TradeTableViewCell.h"
  9. #import "NIMAvatarImageView.h"
  10. #import "UIView+NTES.h"
  11. #import "UIView+Toast.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "User.h"
  14. #import "ImagePagerViewController.h"
  15. #import "HttpRequest.h"
  16. @interface TradeTableViewCell() <UITextViewDelegate>
  17. @property (nonatomic, strong) NIMAvatarImageView *avatarView;
  18. @property (nonatomic, strong) UITextView *nickTextView;
  19. @property (nonatomic, strong) UILabel *timeLabel;
  20. @property (nonatomic, strong) UIView *followContainer;
  21. @property (nonatomic, strong) UILabel *followLabel;
  22. @property (nonatomic, strong) UITextView *msgTextView;
  23. @property (nonatomic, strong) UIView *imgContainer;
  24. @property (nonatomic, strong) UIView *actionContainer;
  25. @property (nonatomic, strong) UIButton *priorityBtn;
  26. @property (nonatomic, strong) TradeData *data;
  27. @end
  28. @implementation TradeTableViewCell
  29. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  30. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  31. if(self)
  32. {
  33. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  34. }
  35. return self;
  36. }
  37. - (void)initWidth:(CGFloat)cellWidth{
  38. float padding = 10;
  39. //avatar
  40. _avatarView = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(padding, padding, 40, 40)];
  41. [_avatarView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  42. [self addSubview:_avatarView];
  43. //nick name
  44. _nickTextView = [[UITextView alloc] initWithFrame:CGRectMake(_avatarView.right + 5, padding, 200, 25)];
  45. _nickTextView.backgroundColor = UIColor.clearColor;
  46. _nickTextView.scrollEnabled = NO;
  47. _nickTextView.editable = NO;
  48. _nickTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
  49. _nickTextView.delegate = self;
  50. _nickTextView.textAlignment = NSTextAlignmentLeft;
  51. [self addSubview:_nickTextView];
  52. //time
  53. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nickTextView.left + 5, _nickTextView.bottom, 200, 15)];
  54. _timeLabel.font = [UIFont systemFontOfSize:14];
  55. _timeLabel.textColor = UIColor.lightGrayColor;
  56. [self addSubview:_timeLabel];
  57. _followContainer = [[UIView alloc] initWithFrame:CGRectMake(cellWidth-padding-40, padding, 50, 40)];
  58. [_followContainer addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchFollow:)]];
  59. [self addSubview:_followContainer];
  60. UIImageView *followIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 20, 20)];
  61. [followIcon setImage:[UIImage imageNamed:@"交易帖_03"]];
  62. [_followContainer addSubview:followIcon];
  63. _followLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, _followContainer.width, 20)];
  64. _followLabel.font = [UIFont systemFontOfSize:13];
  65. _followLabel.textColor = UIColor.lightGrayColor;
  66. _followLabel.textAlignment = NSTextAlignmentCenter;
  67. [_followContainer addSubview:_followLabel];
  68. //message
  69. _msgTextView = [[UITextView alloc] initWithFrame:CGRectMake(padding, _avatarView.bottom+5, cellWidth - padding * 2, 0)];
  70. _msgTextView.font = [UIFont systemFontOfSize:15];
  71. _msgTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
  72. [self addSubview:_msgTextView];
  73. //image container
  74. _imgContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _msgTextView.bottom, cellWidth - padding * 2, 0)];
  75. [self addSubview:_imgContainer];
  76. //action buttons
  77. _actionContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _imgContainer.bottom, cellWidth, 40)];
  78. [self addSubview:_actionContainer];
  79. _priorityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  80. _priorityBtn.frame = CGRectMake(padding, 8, 80, 24);
  81. _priorityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  82. _priorityBtn.layer.cornerRadius = 12;
  83. _priorityBtn.layer.borderWidth = 1;
  84. _priorityBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  85. [_priorityBtn setTitleColor:User.greenColor forState:UIControlStateNormal];
  86. [_actionContainer addSubview:_priorityBtn];
  87. UIView *step = [[UIView alloc] initWithFrame:CGRectMake(0, 39, cellWidth, 0.5)];
  88. step.backgroundColor = UIColor.lightGrayColor;
  89. [_actionContainer addSubview:step];
  90. }
  91. - (void)setData:(TradeData *)data{
  92. _data = data;
  93. NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:data.userId];
  94. if(user)
  95. {
  96. NSURL *url = user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
  97. [_avatarView nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  98. _avatarView.tag = [data.userId intValue];
  99. NSString *nick = user && user.userInfo.nickName ? user.userInfo.nickName : data.userId;
  100. NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
  101. [UIFont systemFontOfSize:15.0], NSFontAttributeName,
  102. UIColorFromRGB(0x4c82b0), NSForegroundColorAttributeName, nil];
  103. NSMutableAttributedString *appendString = [[NSMutableAttributedString alloc] initWithString:nick attributes:attrs];
  104. NSRange range = NSMakeRange(0, appendString.length);
  105. [appendString addAttribute:NSLinkAttributeName value:user.userId range:range];
  106. _nickTextView.attributedText = appendString;
  107. }
  108. [self updateFollow];
  109. _timeLabel.text = data.time;
  110. _msgTextView.text = data.msg;
  111. _msgTextView.height = _msgTextView.contentSize.height;
  112. _imgContainer.top = _msgTextView.bottom + 5;
  113. float imgPadding = 5;
  114. float imgSize = (_imgContainer.width - imgPadding * 2) / 3;
  115. float imgContainerHeight = 0;
  116. int i=0;
  117. for(i=0; i<data.thumbArr.count; i++)
  118. {
  119. float startX = (i % 3) * (imgSize + imgPadding);
  120. float startY = (i / 3) * (imgSize + imgPadding);
  121. imgContainerHeight = startY + imgSize;
  122. UIImageView *img = nil;
  123. if(i >= _imgContainer.subviews.count)
  124. {
  125. img = [[UIImageView alloc] init];
  126. img.userInteractionEnabled = YES;
  127. [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchImage:)]];
  128. [_imgContainer addSubview:img];
  129. }
  130. else
  131. {
  132. img = [_imgContainer.subviews objectAtIndex:i];
  133. }
  134. [img setHidden:NO];
  135. img.frame = CGRectMake(startX, startY, imgSize, imgSize);
  136. NSString *path = [data.thumbArr objectAtIndex:i];
  137. NSURL *url = [NSURL URLWithString:path];
  138. [img sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"聊天-_17"]];
  139. [img setTag:i];
  140. }
  141. for(int j=i; j<_imgContainer.subviews.count; j++)
  142. {
  143. [[_imgContainer.subviews objectAtIndex:j] setHidden:YES];
  144. }
  145. _imgContainer.height = imgContainerHeight;
  146. _actionContainer.top = _imgContainer.bottom;
  147. [_priorityBtn setTitle:[NSString stringWithFormat:@"置顶%d", data.priority] forState:UIControlStateNormal];
  148. [_priorityBtn sizeToFit];
  149. _priorityBtn.width = _priorityBtn.width + 12;
  150. _priorityBtn.height = 24;
  151. data.cellHeight = _actionContainer.bottom + 10;
  152. }
  153. - (void)onTouchFollow:(UITapGestureRecognizer *)gestureRecognizer{
  154. if(_data.followed)
  155. {
  156. [[HttpRequest shared] tradeDeleteFollow:_data.tradeId success:^{
  157. [self.viewController.view makeToast:@"取消关注成功" duration:2 position:CSToastPositionCenter];
  158. _data.followed = NO;
  159. [self updateFollow];
  160. } failure:^{
  161. [self.viewController.view makeToast:@"取消关注失败" duration:2 position:CSToastPositionCenter];
  162. }];
  163. }
  164. else
  165. {
  166. [[HttpRequest shared] tradeFollow:_data.tradeId success:^{
  167. [self.viewController.view makeToast:@"关注成功" duration:2 position:CSToastPositionCenter];
  168. _data.followed = YES;
  169. [self updateFollow];
  170. } failure:^{
  171. [self.viewController.view makeToast:@"关注失败" duration:2 position:CSToastPositionCenter];
  172. }];
  173. }
  174. }
  175. - (void)updateFollow{
  176. if(_data.followed)
  177. {
  178. _followLabel.text = @"已关注";
  179. }
  180. else
  181. {
  182. _followLabel.text = @"关注";
  183. }
  184. }
  185. - (void)onTouchAvatar:(UITapGestureRecognizer *)gestureRecognizer{
  186. UIView *viewClicked = [gestureRecognizer view];
  187. [User showUserInfo:[NSString stringWithFormat:@"%ld", (long)viewClicked.tag]];
  188. }
  189. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
  190. {
  191. [User showUserInfo:url.absoluteString];
  192. return NO;
  193. }
  194. - (void)onTouchImage:(UITapGestureRecognizer *)gestureRecognizer{
  195. UIView *viewClicked = [gestureRecognizer view];
  196. NSLog(@"show image %ld", (long)viewClicked.tag);
  197. ImagePagerViewController *imagePageVC = [[ImagePagerViewController alloc] init];
  198. [imagePageVC setImages:_data.picArr thumbs:_data.thumbArr startIndex:(int)viewClicked.tag];
  199. [self.viewController.navigationController pushViewController:imagePageVC animated:YES];
  200. }
  201. @end