123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- //
- // MyTradeTableViewCell.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/7/4.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "MyTradeTableViewCell.h"
- #import "NIMAvatarImageView.h"
- #import "UIView+NTES.h"
- #import "UIView+Toast.h"
- #import "UIImageView+WebCache.h"
- #import "User.h"
- #import "ImagePagerViewController.h"
- #import "HttpRequest.h"
- @interface MyTradeTableViewCell() <UITextViewDelegate>
- @property (nonatomic, strong) NIMAvatarImageView *avatarView;
- @property (nonatomic, strong) UITextView *nickTextView;
- @property (nonatomic, strong) UILabel *timeLabel;
- @property (nonatomic, strong) UIView *followContainer;
- @property (nonatomic, strong) UILabel *followLabel;
- @property (nonatomic, strong) UITextView *msgTextView;
- @property (nonatomic, strong) UIView *imgContainer;
- @property (nonatomic, strong) UIView *actionContainer;
- @property (nonatomic, strong) UIButton *priorityBtn;
- @property (nonatomic, strong) UILabel *followCountLabel;
- @property (nonatomic, strong) TradeData *data;
- @end
- @implementation MyTradeTableViewCell
- - (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.contentView 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.contentView 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.contentView addSubview:_timeLabel];
-
-
- _followContainer = [[UIView alloc] initWithFrame:CGRectMake(cellWidth-padding-40, padding, 50, 40)];
- [_followContainer addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchFollow:)]];
- [self.contentView addSubview:_followContainer];
-
- UIImageView *followIcon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 20, 20)];
- [followIcon setImage:[UIImage imageNamed:@"交易帖_03"]];
- [_followContainer addSubview:followIcon];
-
- _followLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, _followContainer.width, 20)];
- _followLabel.font = [UIFont systemFontOfSize:13];
- _followLabel.textColor = UIColor.lightGrayColor;
- _followLabel.textAlignment = NSTextAlignmentCenter;
- [_followContainer addSubview:_followLabel];
-
-
- //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.contentView addSubview:_msgTextView];
-
- //image container
- _imgContainer = [[UIView alloc] initWithFrame:CGRectMake(padding, _msgTextView.bottom, cellWidth - padding * 2, 0)];
- _imgContainer.backgroundColor = UIColor.whiteColor;
- [_imgContainer setClipsToBounds:YES];
- [self.contentView addSubview:_imgContainer];
-
- //action buttons
- _actionContainer = [[UIView alloc] initWithFrame:CGRectMake(0, _imgContainer.bottom, cellWidth, 40)];
- [self.contentView addSubview:_actionContainer];
-
- _priorityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _priorityBtn.frame = CGRectMake(padding, 8, 80, 24);
- _priorityBtn.titleLabel.font = [UIFont systemFontOfSize:14];
- _priorityBtn.layer.cornerRadius = 12;
- _priorityBtn.layer.borderWidth = 1;
- _priorityBtn.layer.borderColor = UIColor.lightGrayColor.CGColor;
- [_priorityBtn setTitleColor:User.greenColor forState:UIControlStateNormal];
- [_actionContainer addSubview:_priorityBtn];
-
- _followCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(cellWidth-90, 8, 80, 24)];
- _followCountLabel.textColor = UIColor.lightGrayColor;
- _followCountLabel.font = [UIFont systemFontOfSize:15];
- _followCountLabel.textAlignment = NSTextAlignmentRight;
- [_actionContainer addSubview:_followCountLabel];
-
- UIView *step = [[UIView alloc] initWithFrame:CGRectMake(0, 39, cellWidth, 0.5)];
- step.backgroundColor = UIColor.lightGrayColor;
- [_actionContainer addSubview:step];
- }
- - (void)setData:(TradeData *)data tradeSort:(TradeSort)tradeSort{
-
- _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_user"]];
- _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;
- }
-
-
-
- [self updateFollow];
-
-
-
- _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;
- int i=0;
- for(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;
-
- if(i >= _imgContainer.subviews.count)
- {
- img = [[UIImageView alloc] init];
- img.userInteractionEnabled = true;
- [_imgContainer addSubview:img];
- }
- else
- {
- img = [_imgContainer.subviews objectAtIndex:i];
- }
-
- img.frame = CGRectMake(startX, startY, imgSize, imgSize);
- [img setHidden:NO];
-
- NSString *path = [data.thumbArr objectAtIndex:i];
- NSURL *url = [NSURL URLWithString:path];
- [img sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"聊天-_17"]];
- img.tag = i;
-
- [img addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTouchImage:)]];
- }
-
- for(int j=i; j<_imgContainer.subviews.count; j++)
- {
- [[_imgContainer.subviews objectAtIndex:j] setHidden:YES];
- }
-
- _imgContainer.height = imgContainerHeight;
- _actionContainer.top = _imgContainer.bottom;
-
- [_priorityBtn setTitle:[NSString stringWithFormat:@"置顶%d", data.priority] forState:UIControlStateNormal];
- [_priorityBtn sizeToFit];
- _priorityBtn.width = _priorityBtn.width + 12;
- _priorityBtn.height = 24;
- [_priorityBtn setHidden: tradeSort == TradeSortMyPublished ? NO : YES];
-
- _followCountLabel.text = [NSString stringWithFormat:@"关注%d", data.followCount];
- [_followCountLabel setHidden: tradeSort == TradeSortMyPublished ? NO : YES];
-
- data.cellHeight = _actionContainer.bottom;
-
- [self setNeedsLayout];
-
- }
- - (void)onTouchFollow:(UITapGestureRecognizer *)gestureRecognizer{
-
- if(_data.followed)
- {
- [[HttpRequest shared] tradeDeleteFollow:_data.tradeId success:^{
- [self makeToast:@"取消关注成功"];
- _data.followed = NO;
- [self updateFollow];
- } failure:^{
- [self makeToast:@"取消关注失败"];
- }];
- }
- else
- {
- [[HttpRequest shared] tradeFollow:_data.tradeId success:^{
- [self makeToast:@"关注成功"];
- _data.followed = YES;
- [self updateFollow];
- } failure:^{
- [self makeToast:@"关注失败"];
- }];
- }
- }
- - (void)updateFollow{
-
- if(_data.followed)
- {
- _followLabel.text = @"已关注";
- }
- else
- {
- _followLabel.text = @"关注";
- }
-
- }
- - (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)layoutSubviews
- {
- [super layoutSubviews];
- float padding = 10;
- _msgTextView.width = self.contentView.width - padding * 2;
- _followContainer.right = self.contentView.width - padding;
- _followCountLabel.right = self.contentView.width - padding;
- _imgContainer.width = self.contentView.width - padding * 2;
-
- }
- @end
|