热更新demo

AnswerIconBaseCell.m 14KB

    // // AnswerIconBaseCell.m // ThePaperBase // // Created by zhousan on 15/11/5. // Copyright © 2015年 scar1900. All rights reserved. // #import "AnswerIconBaseCell.h" #import "AnswerIconCommentCell.h" #import "AnswerIconAskCell.h" #import "DetailAnswerIcon.h" #import "TopicAnserIconCell.h" @interface AnswerIconBaseCell() { userBO *user; NSString *nameStr; NSString *praiseNum; } @end @implementation AnswerIconBaseCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self addSubview:self.backView]; [self.backView addSubview:self.anserNameLabel]; [self.backView addSubview:self.backBtn]; [self.backView addSubview:self.OkButton]; [self.backView addSubview:self.timeLabel]; [self.backView addSubview:self.timeImg]; [self.backView addSubview:self.hostLabel]; [self.backView addSubview:self.answerIcon]; [self.backView addSubview:self.iconBack]; [self.backView addSubview:self.verifyTag]; [self.backView addSubview:self.iconButton]; [self.backView addSubview:self.nameButton]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; [self subLayoutSubViews]; } return self; } - (void)needrefreshNightMode:(id)sender{ self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _anserNameLabel.textColor = [UIColor colorWithHexString:TextBlack]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _iconBack.image = [TPUserDefault instance].isNightMode.intValue >0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (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]:@""; praiseNum = data.praiseTimes; NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(readyToReLayout) object:nil]; [[TPUserDefault instance].globalQueue addOperation:operation]; }); } - (void)readyToReLayout { [self performSelectorOnMainThread:@selector(updateCellUI) withObject:nil waitUntilDone:YES]; // usleep(1); } - (void)updateCellUI { self.anserNameLabel.text = nameStr; self.timeLabel.text = _commentBO.pubTime; if (!isBlankString(praiseNum)) { self.OkButton.text = praiseNum; } if (_commentBO.isPraised && [_commentBO.isPraised intValue] == 1) { [self.OkButton setSelected:YES]; }else { [self.OkButton setSelected:NO]; } [self reLayoutSubViews]; } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; // _backView.backgroundColor = [UIColor redColor]; } return _backView; } - (UIButton *)iconButton { if (!_iconButton) { _iconButton = [[UIButton alloc] initWithFrame:CGRectZero]; [_iconButton addTarget:self action:@selector(iconButtonClick) forControlEvents:UIControlEventTouchUpInside]; } return _iconButton; } - (UIButton *)nameButton { if (!_nameButton) { _nameButton = [[UIButton alloc] initWithFrame:CGRectZero]; [_nameButton addTarget:self action:@selector(iconButtonClick) forControlEvents:UIControlEventTouchUpInside]; } return _nameButton; } - (void)iconButtonClick { if ([self.delegate respondsToSelector:@selector(gotoUserInfo:)]) { [self.delegate gotoUserInfo:self.commentBO]; } } - (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 *)hostLabel { if (!_hostLabel) { _hostLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 15, 15)]; _hostLabel.text = @"题主"; _hostLabel.textAlignment = NSTextAlignmentCenter; _hostLabel.backgroundColor = [UIColor colorWithHexString:@"0xec1c24"]; _hostLabel.textColor = [UIColor whiteColor]; _hostLabel.layer.masksToBounds = YES; _hostLabel.layer.cornerRadius = 2; _hostLabel.font = appFont(TEXT_SEVEN_LEVELSIZE, NO); _hostLabel.hidden = YES; } return _hostLabel; } - (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; } - (UILabel*)anserNameLabel { if (!_anserNameLabel) { _anserNameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _anserNameLabel.textColor = [UIColor colorWithHexString:TextBlack]; _anserNameLabel.textAlignment = NSTextAlignmentLeft; _anserNameLabel.lineBreakMode = NSLineBreakByTruncatingTail; _anserNameLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); } return _anserNameLabel; } - (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; } - (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; } - (commentOkButton*)OkButton { if (!_OkButton) { _OkButton = [[commentOkButton alloc]initWithFrame:CGRectZero]; [_OkButton addTarget:self action:@selector(commentOK:) forControlEvents:UIControlEventTouchUpInside]; } return _OkButton; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] initWithFrame:CGRectZero]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _timeLabel.backgroundColor = [UIColor clearColor]; _timeLabel.font = appFont(TEXT_SEVEN_LEVELSIZE, NO); } return _timeLabel; } - (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; } - (UIImageView *)timeImg { if (!_timeImg) { _timeImg = [[UIImageView alloc] initWithFrame:CGRectZero]; _timeImg.image = Image(@"topic/timeImg.png"); } return _timeImg; } - (void)gotoHotAskWithVO:(id)sender { UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } if ([self.delegate respondsToSelector:@selector(gotoHotAsk:)]) { [self.delegate gotoHotAsk:self.commentBO]; } } - (void)commentOK:(UIButton*)btn { if (btn.selected) { return; } NSLog(@"%@", self.class); if ([self isKindOfClass:[AnswerIconBaseCell class]] || [self isKindOfClass:[AnswerIconCommentCell class]] || [self isKindOfClass:[AnswerIconAskCell class]] || [self isKindOfClass:[DetailAnswerIcon class]]) { [MobClick event:@"51"]; } if ([self isKindOfClass:[TopicAnserIconCell class]] || [self isKindOfClass:[AnswerIconCommentCell class]]) { [MobClick event:@"70"]; } [self.OkButton setSelected:YES animated:YES]; NSDictionary *dic = @{@"commentId":self.commentBO.commentId}; __weak typeof(self) Self = self; [Remote doJsonActionWithBlock:2000 requestUrl:commentPraiseURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSString *praseNumStr = responseData[@"praiseTimes"]; if ([responseData[@"praiseTimes"] integerValue] < 1001) { NSString *praseNum = [NSString stringWithFormat:@"%@",responseData[@"praiseTimes"]]; Self.OkButton.text = praseNum; Self.commentBO.praiseTimes = praseNumStr; TPLOG(@"点赞成功"); } Self.commentBO.isPraised = @"1"; if ([Self.delegate respondsToSelector:@selector(hasPraisedAnswer:withCommentBO:)]) { [Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO]; } TPLOG(@"点赞成功"); }else { TPLOG(@"点赞失败"); } }]; } - (void)subLayoutSubViews { __weak typeof(self) weakSelf = self; [self.backView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.timeImg makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.anserNameLabel.bottom).offset(7); make.height.mas_equalTo(10); make.width.mas_equalTo(10); }]; [self.nameButton makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.anserNameLabel); make.bottom.equalTo(self.anserNameLabel); make.height.mas_equalTo(25); make.width.equalTo(self.anserNameLabel); }]; [self.timeLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.timeImg.right).offset(5); make.top.equalTo(weakSelf.anserNameLabel.bottom).offset(8); make.height.mas_equalTo(9); make.width.mas_equalTo(rect_screen.size.width/2); }]; [self.OkButton makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.right).offset(-40); make.top.equalTo(weakSelf.anserNameLabel.top).offset(-2); make.width.mas_equalTo(30); make.height.mas_equalTo(35); }]; [self.hostLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.anserNameLabel.right).offset(5); make.top.equalTo(weakSelf.anserNameLabel.top).offset(2); make.width.mas_equalTo(27); make.height.mas_equalTo(12); }]; } - (void)reLayoutSubViews { CGFloat nameWidth = returnTextWidthWithRTLabel(self.anserNameLabel.text, 12, appFont(TEXT_SIX_LEVELSIZE, NO), 0); if (nameWidth > rect_screen.size.width-110) { nameWidth = rect_screen.size.width-110; } [self.anserNameLabel remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.backView.top).offset(14); make.left.equalTo(self.backView.left).offset(10); make.width.mas_equalTo(nameWidth+40); make.height.mas_equalTo(14); }]; } @end