SocialDetailTableViewCell.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // SocialDetailTableViewCell.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/6/28.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "SocialDetailTableViewCell.h"
  9. #import "NIMAvatarImageView.h"
  10. #import "UIView+NTES.h"
  11. #import "UIImageView+WebCache.h"
  12. #import "User.h"
  13. #import "ImagePagerViewController.h"
  14. #import "HttpRequest.h"
  15. #import "SocialDetailViewController.h"
  16. @interface SocialDetailTableViewCell() <UITextViewDelegate>
  17. @property (nonatomic, strong) NIMAvatarImageView *avatarView;
  18. @property (nonatomic, strong) UITextView *nickTextView;
  19. @property (nonatomic, strong) UILabel *timeLabel;
  20. @property (nonatomic, strong) UITextView *msgTextView;
  21. @property (nonatomic, strong) UIView *imgContainer;
  22. @property (nonatomic, strong) UIView *actionContainer;
  23. @property (nonatomic, strong) UIButton *locationBtn;
  24. @property (nonatomic, strong) UIButton *viewsBtn;
  25. @property (nonatomic, strong) UIButton *likeBtn;
  26. @property (nonatomic, strong) UIButton *commentBtn;
  27. @property (nonatomic, strong) UIView *likeTitleContainer;
  28. @property (nonatomic, strong) UILabel *likeTitle;
  29. @property (nonatomic, strong) UIView *likeContainer;
  30. @property (nonatomic, strong) UIView *commentTitleContainer;
  31. @property (nonatomic, strong) UILabel *commentTitle;
  32. @property (nonatomic, strong) SocialItemData *data;
  33. @property (nonatomic, strong) NSMutableArray *likeArr;
  34. @end
  35. @implementation SocialDetailTableViewCell
  36. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
  37. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  38. if(self)
  39. {
  40. [self setSelectionStyle:UITableViewCellSelectionStyleNone];
  41. }
  42. return self;
  43. }
  44. - (void)initWidth:(CGFloat)cellWidth{
  45. float padding = 10;
  46. //avatar
  47. _avatarView = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(padding, padding, 40, 40)];
  48. [_avatarView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  49. [self addSubview:_avatarView];
  50. //nick name
  51. _nickTextView = [[UITextView alloc] initWithFrame:CGRectMake(_avatarView.right + 5, padding, 200, 25)];
  52. _nickTextView.backgroundColor = UIColor.clearColor;
  53. _nickTextView.scrollEnabled = NO;
  54. _nickTextView.editable = NO;
  55. _nickTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
  56. _nickTextView.delegate = self;
  57. _nickTextView.textAlignment = NSTextAlignmentLeft;
  58. [self addSubview:_nickTextView];
  59. //time
  60. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nickTextView.left + 5, _nickTextView.bottom, 200, 15)];
  61. _timeLabel.font = [UIFont systemFontOfSize:14];
  62. _timeLabel.textColor = UIColor.lightGrayColor;
  63. [self addSubview:_timeLabel];
  64. //message
  65. _msgTextView = [[UITextView alloc] initWithFrame:CGRectMake(padding, _avatarView.bottom+5, cellWidth - padding * 2, 0)];
  66. _msgTextView.font = [UIFont systemFontOfSize:15];
  67. _msgTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
  68. [self addSubview:_msgTextView];
  69. //image container
  70. _imgContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _msgTextView.bottom, cellWidth - padding * 2, 0)];
  71. [self addSubview:_imgContainer];
  72. //action buttons
  73. _actionContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _imgContainer.bottom, cellWidth, 0)];
  74. [self addSubview:_actionContainer];
  75. _locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  76. _locationBtn.frame = CGRectMake(padding, 8, 80, 24);
  77. [_locationBtn setImage:[UIImage imageNamed:@"朋友圈发布_03"] forState:UIControlStateNormal];
  78. _locationBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  79. _locationBtn.layer.cornerRadius = 12;
  80. _locationBtn.layer.borderWidth = 1;
  81. _locationBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
  82. [_locationBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  83. [_actionContainer addSubview:_locationBtn];
  84. CGFloat aBtnWidth = 55;
  85. CGFloat aBtnHeight = 40;
  86. _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  87. [_commentBtn setFrame:CGRectMake(_actionContainer.width-aBtnWidth-padding, 0, aBtnWidth, aBtnHeight)];
  88. _commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  89. [_commentBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  90. [_commentBtn setImage:[UIImage imageNamed:@"朋友圈_09"] forState:UIControlStateNormal];
  91. [_commentBtn setTitle:@"评论" forState:UIControlStateNormal];
  92. [_commentBtn addTarget:self action:@selector(onTouchComment:) forControlEvents:UIControlEventTouchUpInside];
  93. [_actionContainer addSubview:_commentBtn];
  94. _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  95. [_likeBtn setFrame:CGRectMake(_commentBtn.left-3-aBtnWidth, 0, aBtnWidth, aBtnHeight)];
  96. _likeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  97. [_likeBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  98. [_likeBtn setImage:[UIImage imageNamed:@"朋友圈_06"] forState:UIControlStateNormal];
  99. [_likeBtn addTarget:self action:@selector(onTouchLike:) forControlEvents:UIControlEventTouchUpInside];
  100. [_actionContainer addSubview:_likeBtn];
  101. _viewsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  102. [_viewsBtn setFrame:CGRectMake(_likeBtn.left-3-aBtnWidth, 0, aBtnWidth, aBtnHeight)];
  103. _viewsBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  104. [_viewsBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
  105. [_viewsBtn setImage:[UIImage imageNamed:@"朋友圈_03"] forState:UIControlStateNormal];
  106. //[_actionContainer addSubview:_viewsBtn];
  107. //like title
  108. _likeTitleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, aBtnHeight, cellWidth, 40)];
  109. _likeTitleContainer.backgroundColor = UIColorFromRGB(0xeeeeee);
  110. [_actionContainer addSubview:_likeTitleContainer];
  111. UIView *likeTitleFlag = [[UIView alloc] initWithFrame:CGRectMake(padding, 5, 3, 16)];
  112. likeTitleFlag.backgroundColor = User.orangeColor;
  113. likeTitleFlag.centerY = _likeTitleContainer.height / 2;
  114. [_likeTitleContainer addSubview:likeTitleFlag];
  115. _likeTitle = [[UILabel alloc] initWithFrame:CGRectMake(likeTitleFlag.right + 5, 5, 200, 20)];
  116. [_likeTitle setTextColor:UIColor.lightGrayColor];
  117. [_likeTitle setFont:[UIFont systemFontOfSize:15]];
  118. _likeTitle.centerY = _likeTitleContainer.height / 2;
  119. [_likeTitleContainer addSubview:_likeTitle];
  120. //like container
  121. _likeContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _likeTitleContainer.bottom+5, cellWidth - padding * 2, 40)];
  122. [_actionContainer addSubview:_likeContainer];
  123. //comment title
  124. _commentTitleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _likeContainer.bottom, cellWidth, 40)];
  125. _commentTitleContainer.backgroundColor = UIColorFromRGB(0xeeeeee);
  126. [_actionContainer addSubview:_commentTitleContainer];
  127. UIView *commentTitleFlag = [[UIView alloc] initWithFrame:CGRectMake(padding, 5, 3, 20)];
  128. commentTitleFlag.backgroundColor = User.orangeColor;
  129. commentTitleFlag.centerY = _commentTitleContainer.height / 2;
  130. [_commentTitleContainer addSubview:commentTitleFlag];
  131. _commentTitle = [[UILabel alloc] initWithFrame:CGRectMake(commentTitleFlag.right + 5, 5, 200, 20)];
  132. [_commentTitle setTextColor:UIColor.lightGrayColor];
  133. [_commentTitle setFont:[UIFont systemFontOfSize:15]];
  134. _commentTitle.centerY = _commentTitleContainer.height / 2;
  135. [_commentTitleContainer addSubview:_commentTitle];
  136. }
  137. - (void)setSocialData:(SocialItemData *)data{
  138. self.data = data;
  139. NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:data.userId];
  140. if(user)
  141. {
  142. NSURL *url = user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
  143. [_avatarView nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  144. _avatarView.tag = [data.userId intValue];
  145. NSString *nick = user && user.userInfo.nickName ? user.userInfo.nickName : data.userId;
  146. NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
  147. [UIFont systemFontOfSize:15.0], NSFontAttributeName,
  148. UIColorFromRGB(0x4c82b0), NSForegroundColorAttributeName, nil];
  149. NSMutableAttributedString *appendString = [[NSMutableAttributedString alloc] initWithString:nick attributes:attrs];
  150. NSRange range = NSMakeRange(0, appendString.length);
  151. [appendString addAttribute:NSLinkAttributeName value:user.userId range:range];
  152. _nickTextView.attributedText = appendString;
  153. }
  154. _timeLabel.text = data.time;
  155. _msgTextView.text = data.msg;
  156. _msgTextView.height = _msgTextView.contentSize.height;
  157. _imgContainer.top = _msgTextView.bottom + 5;
  158. float imgPadding = 2;
  159. float imgSize = (_imgContainer.width - imgPadding * 2) / 3;
  160. float imgContainerHeight = 0;
  161. for(int i=0; i<data.thumbArr.count; i++)
  162. {
  163. float startX = (i % 3) * (imgSize + imgPadding);
  164. float startY = (i / 3) * (imgSize + imgPadding);
  165. imgContainerHeight = startY + imgSize;
  166. UIImageView *img = nil;
  167. img = [[UIImageView alloc] init];
  168. [_imgContainer addSubview:img];
  169. img.frame = CGRectMake(startX, startY, imgSize, imgSize);
  170. img.userInteractionEnabled = true;
  171. NSString *path = [data.thumbArr objectAtIndex:i];
  172. NSURL *url = [NSURL URLWithString:path];
  173. [img sd_setImageWithURL:url];
  174. img.tag = i;
  175. [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchImage:)]];
  176. }
  177. _imgContainer.height = imgContainerHeight;
  178. User *myUser = [User sharedInfo];
  179. _actionContainer.top = _imgContainer.bottom;
  180. [_locationBtn setTitle:[User distance:data.lat originLng:data.lng targetLat:myUser.latitude targetLng:myUser.longitude] forState:UIControlStateNormal];
  181. [_locationBtn sizeToFit];
  182. _locationBtn.width = _locationBtn.width + 12;
  183. _locationBtn.height = 24;
  184. [_viewsBtn setTitle:[NSString stringWithFormat:@"%d", data.views] forState:UIControlStateNormal];
  185. [_likeBtn setTitle:[NSString stringWithFormat:@"%d", data.likes] forState:UIControlStateNormal];
  186. if(data.likes > 0)
  187. {
  188. [_likeContainer setHidden:NO];
  189. [_likeTitleContainer setHidden:NO];
  190. [_likeTitle setText:[NSString stringWithFormat:@"%d人已赞", data.likes]];
  191. float likeSize = _likeContainer.width / 8;
  192. _likeContainer.height = ceilf(data.likes / 8.0) * likeSize;
  193. [self fetchLikes];
  194. _commentTitleContainer.top = _likeContainer.bottom + 5;
  195. }
  196. else
  197. {
  198. [_likeTitleContainer setHidden:YES];
  199. [_likeContainer setHidden:YES];
  200. _commentTitleContainer.top = _likeTitleContainer.top;
  201. }
  202. [_commentTitle setText:[NSString stringWithFormat:@"全部评论 (%d)", data.comments]];
  203. _actionContainer.height = _commentTitleContainer.bottom;
  204. _cellHeight = _actionContainer.bottom;
  205. }
  206. - (void)fetchLikes{
  207. if(_likeArr)
  208. return;
  209. [[HttpRequest shared] socialLikeList:_data.socialId success:^(NSMutableArray * _Nullable dataList) {
  210. _likeArr = dataList;
  211. float likeSize = _likeContainer.width / 8;
  212. float likePadding = 2;
  213. for(int i=0; i<dataList.count; i++)
  214. {
  215. NSString *userId = [dataList objectAtIndex:i];
  216. NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:userId];
  217. float startX = (i % 8) * likeSize;
  218. float startY = floorf(i / 8) * likeSize;
  219. NIMAvatarImageView *avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(startX+likePadding, startY+likePadding, likeSize-likePadding*2, likeSize-likePadding*2)];
  220. avatar.tag = [userId intValue];
  221. [avatar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
  222. NSURL *url = user && user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
  223. [avatar nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
  224. [_likeContainer addSubview:avatar];
  225. }
  226. } failure:^{
  227. }];
  228. }
  229. - (void)onTouchAvatar:(UITapGestureRecognizer *)gestureRecognizer{
  230. UIView *viewClicked = [gestureRecognizer view];
  231. [User showUserInfo:[NSString stringWithFormat:@"%ld", (long)viewClicked.tag]];
  232. }
  233. - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
  234. {
  235. [User showUserInfo:url.absoluteString];
  236. return NO;
  237. }
  238. - (void)onTouchImage:(UITapGestureRecognizer *)gestureRecognizer{
  239. UIView *viewClicked = [gestureRecognizer view];
  240. NSLog(@"show image %ld", (long)viewClicked.tag);
  241. ImagePagerViewController *imagePageVC = [[ImagePagerViewController alloc] init];
  242. [imagePageVC setImages:_data.picArr thumbs:_data.thumbArr startIndex:(int)viewClicked.tag];
  243. [self.viewController.navigationController pushViewController:imagePageVC animated:YES];
  244. }
  245. - (void)onTouchComment:(int)sender{
  246. SocialDetailViewController *vc = (SocialDetailViewController *)self.viewController;
  247. if(vc)
  248. {
  249. [vc beginComment:nil];
  250. }
  251. }
  252. - (void)onTouchLike:(int)sender{
  253. [[HttpRequest shared] socialLike:_data.socialId success:^(NSString * _Nullable socialId, int currentLikes) {
  254. _data.likes = currentLikes;
  255. } failure:^{
  256. }];
  257. }
  258. @end