MyTradeTableViewCell.m 10 KB

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