TradeTableViewCell.m 11 KB

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