热更新demo

AnswerContentBaseCell.m 11KB

    // // AnswerContentBaseCell.m // ThePaperBase // // Created by zhousan on 15/11/5. // Copyright © 2015年 scar1900. All rights reserved. // #import "AnswerContentBaseCell.h" #import "AnswerContentCommentCell.h" #import "AnswerContentAskCell.h" #import "DetailAnswerContCell.h" @interface AnswerContentBaseCell() { CGFloat trueContentHeight; CGFloat fourLine; userBO *user; } @end @implementation AnswerContentBaseCell - (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.aswerContentLabel]; [self.backView addSubview:self.menuButton]; [self.backView addSubview:self.expandBtn]; self.lineView = [[UIView alloc] initWithFrame:CGRectZero]; self.lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [self.backView addSubview:self.lineView]; _isFirst = NO; [self subLayoutSubViews]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } return self; } - (void)needrefreshNightMode:(id)sender{ self.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; _lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _aswerContentLabel.textColor = [UIColor colorWithHexString:TextGray]; //个人主页(我的提问,关注的问题,私信等):个人主页(我的提问,关注的问题,私信等)进入原新闻切换成夜间模式返回,显示异常(bug:6153) self.lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [self.aswerContentLabel setNeedsDisplay]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ trueContentHeight= (int)self.commentBO.labelHeight; fourLine = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE); if (!data.isExpand || [data.isExpand intValue] == 0) { _cellHeight = trueContentHeight>fourLine?fourLine:trueContentHeight; }else { _cellHeight = trueContentHeight; } user = setJsonDicToDataModel(data.userInfo, [userBO class]); 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.aswerContentLabel.text = [_commentBO.content mutableCopy]; if (trueContentHeight > fourLine) { self.expandBtn.hidden = NO; }else self.expandBtn.hidden = YES; if (_commentBO.quoteInfo.allKeys.count>0 || _commentBO.imageList.count > 0 || [_commentBO.hasMore isEqualToString:@"1"]) {//TODO当有图片的时候也应该隐藏线 self.lineView.hidden = YES; }else self.lineView.hidden = NO; if (self.shouldNoHiddenLine != nil) { if ([self.shouldNoHiddenLine isEqualToString:@"1"]) { self.lineView.hidden = NO; }else self.lineView.hidden = YES; } if ([_commentBO.hiddenLine isEqualToString:@"1"]) { self.lineView.hidden = YES; } if ([_topicHostId isEqualToString:user.userId] && _isFirst) { self.aswerContentLabel.textColor = [UIColor colorWithHexString:ANSWERBLUECOLOR]; }else { self.aswerContentLabel.textColor = [UIColor colorWithHexString:TextGray]; } [self reLayoutSubViews]; } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; // _backView.backgroundColor = [UIColor greenColor]; } return _backView; } - (TPEmojiLabel *)aswerContentLabel { if (!_aswerContentLabel) { _aswerContentLabel = [[TPEmojiLabel alloc] initWithFrame:CGRectMake(0, 0, rect_screen.size.width-20, 0) LineSpace:10 ParagraphSpacing:0]; _aswerContentLabel.textAlignment = NSTextAlignmentLeft; _aswerContentLabel.lineBreakMode = NSLineBreakByWordWrapping; _aswerContentLabel.textColor = [UIColor colorWithHexString:TextGray]; _aswerContentLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); // _aswerContentLabel.backgroundColor = [UIColor greenColor]; } return _aswerContentLabel; } - (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*)expandBtn { if (!_expandBtn) { _expandBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _expandBtn.backgroundColor = [UIColor clearColor]; _expandBtn.hidden = YES; [_expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal]; [_expandBtn addTarget:self action:@selector(expandEvent:) forControlEvents:UIControlEventTouchUpInside]; _expandBtn.selected = NO; // _expandBtn.backgroundColor = [UIColor greenColor]; } return _expandBtn; } - (void)expandCell:(void (^)(BOOL, CGFloat))block { expandCell = [block copy]; } - (void)expandEvent:(UIButton*)btn { if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { self.commentBO.isExpand = @"1"; }else { self.commentBO.isExpand = @"0"; } CGFloat answerContentHeight = (int)self.commentBO.labelHeight; if (expandCell) { expandCell([self.commentBO.isExpand boolValue],answerContentHeight); } } - (void)subLayoutSubViews { __weak typeof(self) weakSelf = self; [self.backView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self.menuButton makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(weakSelf.aswerContentLabel); }]; [self.expandBtn makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left); make.right.equalTo(weakSelf.backView.right); make.top.equalTo(weakSelf.aswerContentLabel.bottom); make.height.mas_equalTo(25); }]; } - (void)reLayoutSubViews { __weak typeof(self) weakSelf = self; CGFloat four = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE); CGFloat answerContentHeight = (int)self.commentBO.labelHeight; if (answerContentHeight > four) { self.expandBtn.hidden = NO; }else self.expandBtn.hidden = YES; if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) { answerContentHeight = answerContentHeight>four?four:answerContentHeight; [_expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal]; }else { [_expandBtn setImage:Image(@"detailPage/expandArrowUp.png") forState:UIControlStateNormal]; } [self.aswerContentLabel remakeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.backView.left).offset(10); make.right.equalTo(weakSelf.backView.right).offset(-10); make.top.equalTo(weakSelf.backView.top); make.height.mas_equalTo(answerContentHeight); }]; [self.lineView makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.backView); make.width.mas_equalTo(rect_screen.size.width-20); make.bottom.equalTo(weakSelf.backView.bottom); make.height.mas_equalTo(1); }]; } - (void)awakeFromNib { // Initialization code } #pragma mark - menu tap handler - (void)menuTap:(UIButton*)btn { [self becomeFirstResponder]; // UIMenuItem *commentBack = [[UIMenuItem alloc] initWithTitle:@"回复"action:@selector(commentBack:)]; UIMenuItem *copy = [[UIMenuItem alloc] initWithTitle:@"复制"action:@selector(copyText:)]; UIMenuItem *praise = [[UIMenuItem alloc] initWithTitle:@"点赞"action:@selector(praise:)]; UIMenuItem *share = [[UIMenuItem alloc] initWithTitle:@"分享"action:@selector(shareTo:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:copy,share, praise, nil]]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; return; } [menu setTargetRect:self.aswerContentLabel.frame inView:self]; [menu setMenuVisible:YES animated:YES]; } #pragma mark - menuDelegate - (BOOL)canBecomeFirstResponder { [super canBecomeFirstResponder]; return YES; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [super canPerformAction:action withSender:sender]; if (action == @selector(copyText:) || action == @selector(praise:) || action == @selector(deleteComment:) || action == @selector(shareTo:)) { return YES; } else { return NO; } } - (void)deleteComment:(id)sender { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认删除此发言?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除",nil]; [alert show]; } - (void)copyText:(id)sender { NSLog(@"%@", self.class); if ([self isKindOfClass:[AnswerContentCommentCell class]]) { [MobClick event:@"39"]; } if ([self isKindOfClass:[AnswerContentAskCell class]]) { [MobClick event:@"39"]; } if ([self isKindOfClass:[DetailAnswerContCell class]]) { [MobClick event:@"39"]; } UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.aswerContentLabel.text; ShowMessage(@"复制成功", YES); } - (void)praise:(UIButton *)sender { if ([self.delegate respondsToSelector:@selector(praiseHandlerWithIndexPath:)]) { [self.delegate praiseHandlerWithIndexPath:self.indexPath]; } // [self commentOK:self.OkButton]; } -(void) shareTo:(id)sender{ if ([self.delegate respondsToSelector:@selector(gotoShare:index:)]) { [self.delegate gotoShare:self.commentBO index:self.indexPath]; } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end