热更新demo

askContentCell.m 17KB

    // // askContentCell.m // ThePaperDemo // // Created by scar1900 on 14-9-28. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "askContentCell.h" #import "SDWebImageManager.h" #import "UIImage+wiRoundedRectImage.h" #define LINEVIEW_TAG 10086 @interface askContentCell() @end @implementation askContentCell @synthesize commentBO = _commentBO; - (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.askAuthorLabel]; [self.backView addSubview:self.askContentLabel]; [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.lineView = [[UIImageView alloc] initWithFrame:CGRectZero]; self.lineView.tag = LINEVIEW_TAG; self.lineView.image = Image(@"topic/dottedLine.png"); [self.backView addSubview:self.lineView]; [self.backView addSubview:self.attentionBtn]; [self.backView addSubview:self.replyButton]; [self.backView addSubview:self.anserNumsLabel]; [self.backView addSubview:self.menuButton]; [self.backView addSubview:self.topicNumsLabel]; [self subLayoutSubViews]; // self.backView.backgroundColor = [UIColor greenColor]; [[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.askContentLabel.textColor = [UIColor colorWithHexString:TextBlack]; self.iconBack.image = [TPUserDefault instance].isNightMode.intValue > 0 ? Image(@"detailPage/icon_night.png") :Image(@"detailPage/icon_sun.png"); [self.askContentLabel setNeedsDisplay]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; if ([TPUserDefault instance].userBO && [self.commentBO.isAttented intValue] == 1) { self.attentionBtn.selected = YES; }else { self.attentionBtn.selected = NO; } dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ userBO *user = setJsonDicToDataModel(data.userInfo, [userBO class]); dispatch_async(dispatch_get_main_queue(), ^{ 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 ([Remote IsEnableWIFI]) { [self updateUserHeadImage:user]; }else { self.answerIcon.image = Image(@"login/loginIcon_s.png"); } } } NSString *nameStr = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@""; if (isBlankString(nameStr)) { self.askAuthorLabel.text = @""; }else { self.askAuthorLabel.text = nameStr; } self.anserNumsLabel.text = data.answerNums?data.answerNums:@""; self.askContentLabel.text = data.content?data.content:@""; self.timeLabel.text = data.pubTime?data.pubTime:@""; self.cellHeight = self.askContentLabel.optimumSize.height; [self reLayoutSubViews]; }); }); } - (void)updateUserHeadImage:(userBO*)user{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ SDWebImageManager *imageManager = [SDWebImageManager sharedManager]; [imageManager downloadImageWithURL:[NSURL URLWithString:user.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)reLayoutSubViews { CGFloat width = returnTextWidthWithRTLabel(self.anserNumsLabel.text, 10, appFont(8, NO), 0)+5; __weak typeof(self) weakSelf = self; [self.anserNumsLabel remakeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.backView.right).offset(-15); make.top.equalTo(weakSelf.backView.top).offset(25); make.height.mas_equalTo(9); make.width.mas_equalTo(width); }]; [self.askContentLabel remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.answerIcon.bottom).offset(10); make.height.mas_equalTo(weakSelf.cellHeight); make.right.equalTo(weakSelf.backView.right).offset(-10); }]; } - (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*)menuButton { if (!_menuButton) { _menuButton = [UIButton buttonWithType:UIButtonTypeCustom]; _menuButton.frame = CGRectZero; _menuButton.backgroundColor = [UIColor clearColor]; [_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside]; } return _menuButton; } - (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 { [self becomeFirstResponder]; if ([self.askContentCellDelgate respondsToSelector:@selector(gotoUserInfo:)]) { [self.askContentCellDelgate gotoUserInfo:self.commentBO]; } } - (void)gotoHotAskWithVO:(id)sender { UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; } if ([self.askContentCellDelgate respondsToSelector:@selector(gotoHotAsk:)]) { [self.askContentCellDelgate gotoHotAsk:self.commentBO]; } } - (RTLabel*)askContentLabel { if (!_askContentLabel) { _askContentLabel = [[RTLabel alloc]initWithFrame:CGRectMake(10, 0, rect_screen.size.width - 20, 0)]; _askContentLabel.textAlignment = RTTextAlignmentLeft; _askContentLabel.lineBreakMode = RTTextLineBreakModeWordWrapping; _askContentLabel.lineSpacing = 10; _askContentLabel.textColor = [UIColor colorWithHexString:TextBlack]; _askContentLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); } return _askContentLabel; } - (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(7); 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.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.askAuthorLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(47); make.top.equalTo(weakSelf.backView.top).offset(5); make.height.mas_equalTo(14); make.width.mas_equalTo(210); }]; // [self.anserNumsLabel makeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(weakSelf.commentImg.right).offset(1); // make.top.equalTo(weakSelf.backView.top).offset(25); // make.height.mas_equalTo(9); // make.width.mas_equalTo(17); // }]; [self.commentImg makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(weakSelf.anserNumsLabel.left); make.top.equalTo(weakSelf.backView.top).offset(23); 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(25); make.height.mas_equalTo(10); make.width.mas_equalTo(10); }]; [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); }]; [self.askContentLabel makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.answerIcon.bottom).offset(10); make.height.mas_equalTo(0); make.right.equalTo(weakSelf.backView.right).offset(-10); }]; [self.lineView makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.top.equalTo(weakSelf.askContentLabel.bottom).offset(14); make.width.equalTo(weakSelf.backView.width).offset(-20); make.height.mas_equalTo(0.5); }]; } @end