// // AskIconBaseCell.m // ThePaperBase // // Created by zhousan on 15/11/10. // Copyright © 2015年 scar1900. All rights reserved. // #import "AskIconBaseCell.h" #import "SDWebImage/SDWebImageManager.h" @interface AskIconBaseCell() { userBO *user; NSString *nameStr; BOOL isEnableWifi; } @end @implementation AskIconBaseCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; // self.backgroundColor = [UIColor redColor]; [self addSubview:self.backView]; [self.backView addSubview:self.askAuthorLabel]; [self.backView addSubview:self.answerIcon]; [self.backView addSubview:self.iconBack]; [self.backView addSubview:self.verifyTag]; [self.backView addSubview:self.timeImg]; [self.backView addSubview:self.timeLabel]; [self.backView addSubview:self.commentImg]; [self.backView addSubview:self.backBtn]; [self.backView addSubview:self.UserInfoBtn]; [self.backView addSubview:self.nameButton]; [self.backView addSubview:self.attentionBtn]; [self.backView addSubview:self.replyButton]; [self.backView addSubview:self.anserNumsLabel]; [self.backView addSubview:self.topicNumsLabel]; [self subLayoutSubViews]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (void)needrefreshNightMode:(id)sender{ self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; [self.attentionBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; self.attentionBtn.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; [self.replyButton setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; self.replyButton.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; self.askAuthorLabel.textColor = [UIColor colorWithHexString:TextBlack]; self.anserNumsLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; self.topicNumsLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; self.iconBack.image = [TPUserDefault instance].isNightMode.intValue > 0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ user = setJsonDicToDataModel(data.userInfo, [userBO class]); nameStr = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@""; isEnableWifi = [Remote IsEnableWIFI]; NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(readyToReLayout) object:nil]; [[TPUserDefault instance].globalQueue addOperation:operation]; }); } - (void)updateUserHeadImage:(userBO*)userBO{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ SDWebImageManager *imageManager = [SDWebImageManager sharedManager]; [imageManager downloadImageWithURL:[NSURL URLWithString:userBO.pic] options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { } completed:^(UIImage *img, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { // UIImage *roundImage = [UIImage circleImage:img withParam:28]; dispatch_async(dispatch_get_main_queue(), ^{ self.answerIcon.image = img; }); }]; }); } - (void)readyToReLayout { [self performSelectorOnMainThread:@selector(updateCellUI) withObject:nil waitUntilDone:YES]; // usleep(1); } - (void)updateCellUI { // dispatch_async(dispatch_get_main_queue(), ^{ if ([TPUserDefault instance].userBO && [self.commentBO.isAttented intValue] == 1) { self.attentionBtn.selected = YES; }else { self.attentionBtn.selected = NO; } if ([user.isAuth integerValue] ==1) {//认证用户提问或回答,头像不显示加”V“(bug:4116) self.verifyTag.hidden = NO; }else{ self.verifyTag.hidden = YES; } if (isBlankString(user.pic)) { self.answerIcon.image = Image(@"login/loginIcon_s.png"); }else { if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [self updateUserHeadImage:user]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { self.answerIcon.image = Image(@"login/loginIcon_s.png"); }else { if (isEnableWifi) { [self updateUserHeadImage:user]; }else { self.answerIcon.image = Image(@"login/loginIcon_s.png"); } } } if (isBlankString(nameStr)) { self.askAuthorLabel.text = @""; }else { self.askAuthorLabel.text = nameStr; } // self.attentionBtn.selected = data.isAttented.boolValue; self.anserNumsLabel.text = _commentBO.answerNums?_commentBO.answerNums:@""; self.timeLabel.text = _commentBO.pubTime?_commentBO.pubTime:@""; [self reLayoutSubViews]; // }); } - (UIView*)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; } return _backView; } - (UIImageView *)commentImg { if (!_commentImg) { _commentImg = [[UIImageView alloc] initWithFrame:CGRectZero]; _commentImg.image = Image(@"live/commentIcon.png"); } return _commentImg; } - (UIImageView *)iconBack { if (!_iconBack) { _iconBack = [[UIImageView alloc] initWithFrame:CGRectZero]; _iconBack.image = [TPUserDefault instance].isNightMode.intValue >0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); } return _iconBack; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _timeLabel.backgroundColor = [UIColor clearColor]; _timeLabel.font = appFont(8, NO); } return _timeLabel; } - (UIButton *)backBtn { if (!_backBtn) { _backBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _backBtn.frame = CGRectZero; _backBtn.backgroundColor = [UIColor clearColor]; [_backBtn addTarget:self action:@selector(gotoHotAskWithVO:) forControlEvents:UIControlEventTouchUpInside]; } return _backBtn; } - (UIButton *)UserInfoBtn { if (!_UserInfoBtn) { _UserInfoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _UserInfoBtn.frame = CGRectZero; _UserInfoBtn.backgroundColor = [UIColor clearColor]; [_UserInfoBtn addTarget:self action:@selector(gotoUserInfo:) forControlEvents:UIControlEventTouchUpInside]; } return _UserInfoBtn; } - (UIButton *)nameButton { if (!_nameButton) { _nameButton = [UIButton buttonWithType:UIButtonTypeCustom]; _nameButton.frame = CGRectZero; [_nameButton addTarget:self action:@selector(gotoUserInfo:) forControlEvents:UIControlEventTouchUpInside]; } return _nameButton; } - (UIButton *)attentionBtn { if (!_attentionBtn) { _attentionBtn = [[UIButton alloc] initWithFrame:CGRectZero]; _attentionBtn.backgroundColor = [UIColor clearColor]; [_attentionBtn addTarget:self action:@selector(attentionBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [_attentionBtn setTitle:@"关注" forState:UIControlStateNormal]; [_attentionBtn setTitle:@"已关注" forState:UIControlStateSelected]; _attentionBtn.titleLabel.font = appFont(10, NO); [_attentionBtn setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; _attentionBtn.layer.cornerRadius = 4; _attentionBtn.layer.borderWidth = 1; _attentionBtn.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; _attentionBtn.hidden = YES; } return _attentionBtn; } - (UIButton *)replyButton { if (!_replyButton) { _replyButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; _replyButton.backgroundColor = [UIColor clearColor]; [_replyButton addTarget:self action:@selector(replyButtonBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [_replyButton setTitle:@"回复" forState:UIControlStateNormal]; [_replyButton setTitle:@"已回复" forState:UIControlStateSelected]; _replyButton.titleLabel.font = appFont(10, NO); [_replyButton setTitleColor:[UIColor colorWithHexString:LIGHTGRAY] forState:UIControlStateNormal]; _replyButton.layer.cornerRadius = 4; _replyButton.layer.masksToBounds = YES; _replyButton.layer.borderWidth = 1; _replyButton.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor; _replyButton.hidden = YES; } return _replyButton; } - (UIImageView *)timeImg { if (!_timeImg) { _timeImg = [[UIImageView alloc] initWithFrame:CGRectZero]; _timeImg.image = Image(@"topic/timeImg.png"); } return _timeImg; } - (UIImageView *)verifyTag { if (!_verifyTag) { _verifyTag = [[UIImageView alloc] initWithFrame:CGRectZero]; _verifyTag.image = Image(@"topic/vipVerify.png"); } return _verifyTag; } - (UIImageView *)answerIcon { if (_answerIcon == nil) { _answerIcon = [[UIImageView alloc] initWithFrame:CGRectZero]; _answerIcon.image = Image(@"login/loginIcon_s.png"); } return _answerIcon; } - (UILabel*)askAuthorLabel { if (!_askAuthorLabel) { _askAuthorLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _askAuthorLabel.textColor = [UIColor colorWithHexString:TextBlack]; _askAuthorLabel.textAlignment = NSTextAlignmentLeft; _askAuthorLabel.font = appFont(11, NO); } return _askAuthorLabel; } - (UILabel*)anserNumsLabel { if (!_anserNumsLabel) { _anserNumsLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _anserNumsLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _anserNumsLabel.textAlignment = NSTextAlignmentRight; _anserNumsLabel.font = appFont(8, NO); } return _anserNumsLabel; } - (UILabel *)topicNumsLabel { if (!_topicNumsLabel) { _topicNumsLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _topicNumsLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _topicNumsLabel.textAlignment = NSTextAlignmentRight; _topicNumsLabel.backgroundColor = [UIColor clearColor]; } _topicNumsLabel.font = appFont(9, NO); return _topicNumsLabel; } #pragma mark - gotoUserInfoBtnClick - (void)gotoUserInfo:(id)sender { if ([self.delegate respondsToSelector:@selector(askIconBaseGotoUserInfo:)]) { [self.delegate askIconBaseGotoUserInfo:self.commentBO]; } } - (void)gotoHotAskWithVO:(id)sender { UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } if ([self.delegate respondsToSelector:@selector(askIconBaseGotoHotAsk:)]) { [self.delegate askIconBaseGotoHotAsk:self.commentBO]; } } - (void)subLayoutSubViews { __weak typeof(self) weakSelf = self; [self.backView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.left); make.top.equalTo(weakSelf.top); make.bottom.equalTo(weakSelf.bottom); make.right.equalTo(weakSelf.right); }]; [self.backBtn makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(weakSelf.backView); }]; [self.answerIcon makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.backView.top).offset(12); make.width.mas_equalTo(28); make.height.mas_equalTo(28); }]; [self.iconBack makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.answerIcon); }]; [self.UserInfoBtn makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(weakSelf.answerIcon); }]; [self.nameButton makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.askAuthorLabel); make.bottom.equalTo(self.askAuthorLabel); make.height.mas_equalTo(25); make.width.equalTo(self.askAuthorLabel); }]; [self.verifyTag makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(28); make.top.equalTo(weakSelf.backView.top).offset(28); make.height.mas_equalTo(12); make.width.mas_equalTo(12); }]; [self.timeImg makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(48); make.top.equalTo(weakSelf.backView.top).offset(30); make.height.mas_equalTo(10); make.width.mas_equalTo(10); }]; [self.commentImg makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.anserNumsLabel.left); make.bottom.equalTo(weakSelf.timeImg.bottom); make.height.mas_equalTo(12); make.width.mas_equalTo(12); }]; [self.timeLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.timeImg.right).offset(5); make.top.equalTo(weakSelf.askAuthorLabel.bottom).offset(7); make.height.mas_equalTo(9); make.width.mas_equalTo(rect_screen.size.width/2); }]; } - (void)reLayoutSubViews { CGFloat width = returnTextWidthWithRTLabel(self.anserNumsLabel.text, 10, appFont(8, NO), 0)+5; CGFloat nameWidth = returnTextWidthWithRTLabel(self.askAuthorLabel.text, 10, appFont(11, NO), 0)+5; __weak typeof(self) weakSelf = self; if (nameWidth > rect_screen.size.width-110) { nameWidth = rect_screen.size.width-110; } [self.anserNumsLabel remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.backView.right).offset(-15); make.bottom.equalTo(weakSelf.timeImg.bottom).offset(-1); make.height.mas_equalTo(9); make.width.mas_equalTo(width); }]; [self.askAuthorLabel remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(47); make.top.equalTo(weakSelf.backView.top).offset(10); make.height.mas_equalTo(14); make.width.mas_equalTo(nameWidth); }]; } @end