123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // SocialDetailTableViewCell.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/6/28.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SocialDetailTableViewCell.h"
- #import "NIMAvatarImageView.h"
- #import "UIView+NTES.h"
- #import "UIImageView+WebCache.h"
- #import "User.h"
- #import "ImagePagerViewController.h"
- #import "HttpRequest.h"
- #import "SocialDetailViewController.h"
- @interface SocialDetailTableViewCell() <UITextViewDelegate>
- @property (nonatomic, strong) NIMAvatarImageView *avatarView;
- @property (nonatomic, strong) UITextView *nickTextView;
- @property (nonatomic, strong) UILabel *timeLabel;
- @property (nonatomic, strong) UITextView *msgTextView;
- @property (nonatomic, strong) UIView *imgContainer;
- @property (nonatomic, strong) UIView *actionContainer;
- @property (nonatomic, strong) UIButton *locationBtn;
- @property (nonatomic, strong) UIButton *viewsBtn;
- @property (nonatomic, strong) UIButton *likeBtn;
- @property (nonatomic, strong) UIButton *commentBtn;
- @property (nonatomic, strong) UIView *likeTitleContainer;
- @property (nonatomic, strong) UILabel *likeTitle;
- @property (nonatomic, strong) UIView *likeContainer;
- @property (nonatomic, strong) UIView *commentTitleContainer;
- @property (nonatomic, strong) UILabel *commentTitle;
- @property (nonatomic, strong) SocialItemData *data;
- @property (nonatomic, strong) NSMutableArray *likeArr;
- @end
- @implementation SocialDetailTableViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if(self)
- {
- [self setSelectionStyle:UITableViewCellSelectionStyleNone];
- }
- return self;
- }
- - (void)initWidth:(CGFloat)cellWidth{
-
- float padding = 10;
-
- //avatar
- _avatarView = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(padding, padding, 40, 40)];
- [_avatarView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
- [self addSubview:_avatarView];
-
- //nick name
- _nickTextView = [[UITextView alloc] initWithFrame:CGRectMake(_avatarView.right + 5, padding, 200, 25)];
- _nickTextView.backgroundColor = UIColor.clearColor;
- _nickTextView.scrollEnabled = NO;
- _nickTextView.editable = NO;
- _nickTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
- _nickTextView.delegate = self;
- _nickTextView.textAlignment = NSTextAlignmentLeft;
- [self addSubview:_nickTextView];
-
- //time
- _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nickTextView.left + 5, _nickTextView.bottom, 200, 15)];
- _timeLabel.font = [UIFont systemFontOfSize:14];
- _timeLabel.textColor = UIColor.lightGrayColor;
- [self addSubview:_timeLabel];
-
- //message
- _msgTextView = [[UITextView alloc] initWithFrame:CGRectMake(padding, _avatarView.bottom+5, cellWidth - padding * 2, 0)];
- _msgTextView.font = [UIFont systemFontOfSize:15];
- _msgTextView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
- [self addSubview:_msgTextView];
-
- //image container
- _imgContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _msgTextView.bottom, cellWidth - padding * 2, 0)];
- [self addSubview:_imgContainer];
-
- //action buttons
- _actionContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _imgContainer.bottom, cellWidth, 0)];
- [self addSubview:_actionContainer];
-
- _locationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _locationBtn.frame = CGRectMake(padding, 8, 80, 24);
- [_locationBtn setImage:[UIImage imageNamed:@"朋友圈发布_03"] forState:UIControlStateNormal];
- _locationBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- _locationBtn.layer.cornerRadius = 12;
- _locationBtn.layer.borderWidth = 1;
- _locationBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
- [_locationBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
- [_actionContainer addSubview:_locationBtn];
-
- CGFloat aBtnWidth = 55;
- CGFloat aBtnHeight = 40;
-
- _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_commentBtn setFrame:CGRectMake(_actionContainer.width-aBtnWidth-padding, 0, aBtnWidth, aBtnHeight)];
- _commentBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- [_commentBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
- [_commentBtn setImage:[UIImage imageNamed:@"朋友圈_09"] forState:UIControlStateNormal];
- [_commentBtn setTitle:@"评论" forState:UIControlStateNormal];
- [_commentBtn addTarget:self action:@selector(onTouchComment:) forControlEvents:UIControlEventTouchUpInside];
- [_actionContainer addSubview:_commentBtn];
-
-
- _likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_likeBtn setFrame:CGRectMake(_commentBtn.left-3-aBtnWidth, 0, aBtnWidth, aBtnHeight)];
- _likeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- [_likeBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
- [_likeBtn setImage:[UIImage imageNamed:@"朋友圈_06"] forState:UIControlStateNormal];
- [_likeBtn addTarget:self action:@selector(onTouchLike:) forControlEvents:UIControlEventTouchUpInside];
- [_actionContainer addSubview:_likeBtn];
-
- _viewsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [_viewsBtn setFrame:CGRectMake(_likeBtn.left-3-aBtnWidth, 0, aBtnWidth, aBtnHeight)];
- _viewsBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- [_viewsBtn setTitleColor:UIColor.lightGrayColor forState:UIControlStateNormal];
- [_viewsBtn setImage:[UIImage imageNamed:@"朋友圈_03"] forState:UIControlStateNormal];
- //[_actionContainer addSubview:_viewsBtn];
-
-
- //like title
- _likeTitleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, aBtnHeight, cellWidth, 40)];
- _likeTitleContainer.backgroundColor = UIColorFromRGB(0xeeeeee);
- [_actionContainer addSubview:_likeTitleContainer];
-
- UIView *likeTitleFlag = [[UIView alloc] initWithFrame:CGRectMake(padding, 5, 3, 16)];
- likeTitleFlag.backgroundColor = User.orangeColor;
- likeTitleFlag.centerY = _likeTitleContainer.height / 2;
- [_likeTitleContainer addSubview:likeTitleFlag];
-
- _likeTitle = [[UILabel alloc] initWithFrame:CGRectMake(likeTitleFlag.right + 5, 5, 200, 20)];
- [_likeTitle setTextColor:UIColor.lightGrayColor];
- [_likeTitle setFont:[UIFont systemFontOfSize:15]];
- _likeTitle.centerY = _likeTitleContainer.height / 2;
- [_likeTitleContainer addSubview:_likeTitle];
-
-
- //like container
- _likeContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _likeTitleContainer.bottom+5, cellWidth - padding * 2, 40)];
- [_actionContainer addSubview:_likeContainer];
-
- //comment title
- _commentTitleContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _likeContainer.bottom, cellWidth, 40)];
- _commentTitleContainer.backgroundColor = UIColorFromRGB(0xeeeeee);
- [_actionContainer addSubview:_commentTitleContainer];
-
- UIView *commentTitleFlag = [[UIView alloc] initWithFrame:CGRectMake(padding, 5, 3, 20)];
- commentTitleFlag.backgroundColor = User.orangeColor;
- commentTitleFlag.centerY = _commentTitleContainer.height / 2;
- [_commentTitleContainer addSubview:commentTitleFlag];
-
- _commentTitle = [[UILabel alloc] initWithFrame:CGRectMake(commentTitleFlag.right + 5, 5, 200, 20)];
- [_commentTitle setTextColor:UIColor.lightGrayColor];
- [_commentTitle setFont:[UIFont systemFontOfSize:15]];
- _commentTitle.centerY = _commentTitleContainer.height / 2;
- [_commentTitleContainer addSubview:_commentTitle];
- }
- - (void)setSocialData:(SocialItemData *)data{
-
- self.data = data;
-
- NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:data.userId];
- if(user)
- {
- NSURL *url = user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
- [_avatarView nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
- _avatarView.tag = [data.userId intValue];
-
-
- NSString *nick = user && user.userInfo.nickName ? user.userInfo.nickName : data.userId;
- NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
- [UIFont systemFontOfSize:15.0], NSFontAttributeName,
- UIColorFromRGB(0x4c82b0), NSForegroundColorAttributeName, nil];
- NSMutableAttributedString *appendString = [[NSMutableAttributedString alloc] initWithString:nick attributes:attrs];
- NSRange range = NSMakeRange(0, appendString.length);
- [appendString addAttribute:NSLinkAttributeName value:user.userId range:range];
- _nickTextView.attributedText = appendString;
- }
-
- _timeLabel.text = data.time;
- _msgTextView.text = data.msg;
-
- _msgTextView.height = _msgTextView.contentSize.height;
-
- _imgContainer.top = _msgTextView.bottom + 5;
- float imgPadding = 2;
- float imgSize = (_imgContainer.width - imgPadding * 2) / 3;
- float imgContainerHeight = 0;
- for(int i=0; i<data.thumbArr.count; i++)
- {
- float startX = (i % 3) * (imgSize + imgPadding);
- float startY = (i / 3) * (imgSize + imgPadding);
- imgContainerHeight = startY + imgSize;
- UIImageView *img = nil;
- img = [[UIImageView alloc] init];
- [_imgContainer addSubview:img];
-
- img.frame = CGRectMake(startX, startY, imgSize, imgSize);
- img.userInteractionEnabled = true;
-
- NSString *path = [data.thumbArr objectAtIndex:i];
- NSURL *url = [NSURL URLWithString:path];
- [img sd_setImageWithURL:url];
- img.tag = i;
-
- [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchImage:)]];
- }
- _imgContainer.height = imgContainerHeight;
-
- User *myUser = [User sharedInfo];
-
- _actionContainer.top = _imgContainer.bottom;
- [_locationBtn setTitle:[User distance:data.lat originLng:data.lng targetLat:myUser.latitude targetLng:myUser.longitude] forState:UIControlStateNormal];
- [_locationBtn sizeToFit];
- _locationBtn.width = _locationBtn.width + 12;
- _locationBtn.height = 24;
-
- [_viewsBtn setTitle:[NSString stringWithFormat:@"%d", data.views] forState:UIControlStateNormal];
- [_likeBtn setTitle:[NSString stringWithFormat:@"%d", data.likes] forState:UIControlStateNormal];
-
-
-
- if(data.likes > 0)
- {
- [_likeContainer setHidden:NO];
- [_likeTitleContainer setHidden:NO];
- [_likeTitle setText:[NSString stringWithFormat:@"%d人已赞", data.likes]];
-
- float likeSize = _likeContainer.width / 8;
- _likeContainer.height = ceilf(data.likes / 8.0) * likeSize;
- [self fetchLikes];
-
- _commentTitleContainer.top = _likeContainer.bottom + 5;
- }
- else
- {
- [_likeTitleContainer setHidden:YES];
- [_likeContainer setHidden:YES];
-
- _commentTitleContainer.top = _likeTitleContainer.top;
- }
-
-
- [_commentTitle setText:[NSString stringWithFormat:@"全部评论 (%d)", data.comments]];
-
- _actionContainer.height = _commentTitleContainer.bottom;
- _cellHeight = _actionContainer.bottom;
- }
- - (void)fetchLikes{
-
- if(_likeArr)
- return;
-
- [[HttpRequest shared] socialLikeList:_data.socialId success:^(NSMutableArray * _Nullable dataList) {
-
- _likeArr = dataList;
-
- float likeSize = _likeContainer.width / 8;
- float likePadding = 2;
-
- for(int i=0; i<dataList.count; i++)
- {
- NSString *userId = [dataList objectAtIndex:i];
- NIMUser *user = [[NIMSDK sharedSDK].userManager userInfo:userId];
-
- float startX = (i % 8) * likeSize;
- float startY = floorf(i / 8) * likeSize;
-
- NIMAvatarImageView *avatar = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(startX+likePadding, startY+likePadding, likeSize-likePadding*2, likeSize-likePadding*2)];
-
- avatar.tag = [userId intValue];
- [avatar addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchAvatar:)]];
-
- NSURL *url = user && user.userInfo.avatarUrl ? [NSURL URLWithString:user.userInfo.avatarUrl] : nil;
- [avatar nim_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar_user2"]];
- [_likeContainer addSubview:avatar];
- }
-
- } failure:^{
-
- }];
-
- }
- - (void)onTouchAvatar:(UITapGestureRecognizer *)gestureRecognizer{
-
- UIView *viewClicked = [gestureRecognizer view];
- [User showUserInfo:[NSString stringWithFormat:@"%ld", (long)viewClicked.tag]];
-
- }
- - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
- {
- [User showUserInfo:url.absoluteString];
- return NO;
- }
- - (void)onTouchImage:(UITapGestureRecognizer *)gestureRecognizer{
-
- UIView *viewClicked = [gestureRecognizer view];
-
- NSLog(@"show image %ld", (long)viewClicked.tag);
-
- ImagePagerViewController *imagePageVC = [[ImagePagerViewController alloc] init];
- [imagePageVC setImages:_data.picArr thumbs:_data.thumbArr startIndex:(int)viewClicked.tag];
-
- [self.viewController.navigationController pushViewController:imagePageVC animated:YES];
- }
- - (void)onTouchComment:(int)sender{
-
- SocialDetailViewController *vc = (SocialDetailViewController *)self.viewController;
- if(vc)
- {
- [vc beginComment:nil];
- }
-
- }
- - (void)onTouchLike:(int)sender{
-
- [[HttpRequest shared] socialLike:_data.socialId success:^(NSString * _Nullable socialId, int currentLikes) {
-
- _data.likes = currentLikes;
-
- } failure:^{
-
- }];
-
- }
- @end
|