// // topicAnswerCell.m // ThePaperHD // // Created by scar1900 on 15/5/12. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "topicAnswerCell.h" #import "TPSelectButton.h" #define tableWidht 1230/2 @interface topicAnswerCell() { CGFloat answerContentHeight; } @property(nonatomic, strong)UIView* backView; @property(nonatomic, strong)UILabel *answerNameLabel; @property(nonatomic, strong)RTLabel *answerContentLabel; @property(nonatomic, strong)TPSelectButton *praiseButton; @property(nonatomic, strong)UILabel *timeLabel; @property(nonatomic, strong)UIView *lineView; @property(nonatomic, strong)UIButton *menuButton; @end @implementation topicAnswerCell @synthesize commentBO = _commentBO; @synthesize padding; @synthesize indexPath; @synthesize delegate; @synthesize askCommentBO; //需要呼出自定义菜单 - (BOOL)canBecomeFirstResponder{ [super canBecomeFirstResponder]; return YES; } - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; [self.contentView addSubview:self.backView]; [self.backView addSubview:self.answerContentLabel]; [self.backView addSubview:self.answerNameLabel]; [self.backView addSubview:self.timeLabel]; [self.backView addSubview:self.lineView]; [self.backView addSubview:self.menuButton]; [self.backView addSubview:self.praiseButton]; } return self; } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ userBO *user = setJsonDicToDataModel(data.userInfo, [userBO class]); NSString *nameStr = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@""; answerContentHeight = returnTextHeightWithRTLabel(data.content, tableWidht, appFont(interactionFontSize, NO), 10); NSString *praiseNum = data.praiseTimes; dispatch_async(dispatch_get_main_queue(), ^{ self.answerNameLabel.text = nameStr; self.answerContentLabel.text = data.content; self.timeLabel.text = data.pubTime; if (!isBlankString(praiseNum)) { self.praiseButton.titleText = praiseNum; } if (data.isPraised && [data.isPraised intValue] == 1) { [self.praiseButton setSelect:YES animation:NO]; }else { [self.praiseButton setSelect:NO animation:NO]; } [self layoutSubviews]; }); }); } - (UIView *)backView { if (!_backView) { _backView = [[UIView alloc]initWithFrame:CGRectZero]; _backView.backgroundColor = [UIColor clearColor]; } return _backView; } - (UILabel *)answerNameLabel { if (!_answerNameLabel) { _answerNameLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _answerNameLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _answerNameLabel.font = appFont(14, NO); _answerNameLabel.textAlignment = NSTextAlignmentLeft; _answerNameLabel.backgroundColor = [UIColor clearColor]; } return _answerNameLabel; } - (RTLabel*)answerContentLabel { if (!_answerContentLabel) { _answerContentLabel = [[RTLabel alloc]initWithFrame:CGRectMake(0, 0, tableWidht, 0)]; _answerContentLabel.textColor = [UIColor colorWithHexString:ANSWERBLUECOLOR]; _answerContentLabel.textAlignment = RTTextAlignmentLeft; _answerContentLabel.lineBreakMode = RTTextLineBreakModeWordWrapping; _answerContentLabel.lineSpacing = 10; _answerContentLabel.backgroundColor = [UIColor clearColor]; _answerContentLabel.font = appFont(interactionFontSize, NO);//【需求变更】话题详情页问答列表的文字字号调整(bug:4445) } return _answerContentLabel; } - (UILabel*)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc]initWithFrame:CGRectZero]; _timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _timeLabel.font = appFont(11, NO); _timeLabel.textAlignment = NSTextAlignmentLeft; _timeLabel.backgroundColor = [UIColor clearColor]; } return _timeLabel; } - (UIView*)lineView { if (!_lineView) { _lineView = [[UIView alloc]initWithFrame:CGRectZero]; _lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; } return _lineView; } - (TPSelectButton*)praiseButton { if (!_praiseButton) { _praiseButton = [[TPSelectButton alloc]initWithFrame:CGRectZero target:self selector:@selector(praiseHandler:)]; [_praiseButton setBUttonNormalImage:Image(@"detailPage/praise_small.png") highLightImage:Image(@"detailPage/praise_small_s.png") selectedImage:Image(@"detailPage/praise_small_s.png")]; _praiseButton.textLabel.font = appFont(11, NO); _praiseButton.topPadding = 5; _praiseButton.textLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY]; } return _praiseButton; } - (void)praiseHandler:(UIButton*)btn { if (btn.selected) { return; } 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) { [MobClick event:@"40"]; NSString *praseNumStr = responseData[@"praiseTimes"]; Self.praiseButton.textLabel.text = praseNumStr; Self.commentBO.isPraised = @"1"; Self.commentBO.praiseTimes = praseNumStr; if ([Self.delegate respondsToSelector:@selector(hasPraisedAnswer:withCommentBO:)]) { [Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO]; } TPLOG(@"点赞成功"); [Self.praiseButton setSelect:YES animation:YES]; }else { ShowMessage(@"点赞失败", NO); } }]; } - (void)layoutSubviews { [super layoutSubviews]; self.backView.frame = CGRectMake(padding, 0, tableWidht, CGRectGetHeight(self.contentView.bounds)); self.answerNameLabel.frame = CGRectMake(0, 15, 1160/2, 20); self.timeLabel.frame = CGRectMake(0, CGRectGetMaxY(self.answerNameLabel.frame)+3, 1160/2, 13); self.answerContentLabel.frame = CGRectMake(0, CGRectGetMaxY(self.timeLabel.frame)+11, tableWidht, answerContentHeight); self.praiseButton.frame =CGRectMake(CGRectGetWidth(self.backView.bounds)-55/2, CGRectGetMinY(self.answerNameLabel.frame), 25, 45); self.lineView.frame = CGRectMake(0, CGRectGetHeight(self.backView.bounds)-1, tableWidht, 1); self.menuButton.frame = self.backView.bounds; } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } - (UIButton*)menuButton { if (!_menuButton) { _menuButton = [UIButton buttonWithType:UIButtonTypeCustom]; _menuButton.backgroundColor = [UIColor clearColor]; [_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside]; } return _menuButton; } - (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:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; return; } [menu setMenuItems:[NSArray arrayWithObjects:commentBack,copy, praise, nil]]; [menu setTargetRect:self.answerContentLabel.frame inView:self]; [menu setMenuVisible:YES animated:YES]; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender { [super canPerformAction:action withSender:sender]; if ( action == @selector(commentBack:) || action == @selector(copyText:) || action == @selector(praise:)) { return YES; } else { return NO; } } - (void)commentBack:(id)sender { if ([self.delegate respondsToSelector:@selector(commentBackForUser:withCommentBO:askBO:)]) { [self.delegate commentBackForUser:self.indexPath withCommentBO:self.commentBO askBO:self.askCommentBO]; } } - (void)copyText:(id)sender { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = self.answerContentLabel.text; ShowMessage(@"复制成功", YES); [MobClick event:@"22"]; } - (void)praise:(id)sender { if (self.praiseButton.button.selected) { return; } 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) { [MobClick event:@"40"]; NSString *praseNumStr = responseData[@"praiseTimes"]; Self.praiseButton.textLabel.text = praseNumStr; Self.commentBO.isPraised = @"1"; Self.commentBO.praiseTimes = praseNumStr; if ([Self.delegate respondsToSelector:@selector(hasPraisedComment:withCommentBO:)]) { [Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO]; } TPLOG(@"点赞成功"); [Self.praiseButton setSelect:YES animation:YES]; }else { ShowMessage(@"点赞失败", NO); } }]; } @end