|
//
// answerContentCell.m
// ThePaperHD
//
// Created by scar1900 on 15/1/27.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "answerContentCell.h"
@interface answerContentCell() {
// CGFloat width;
}
@end
@implementation answerContentCell
@synthesize commentBO = _commentBO;
@synthesize answerContentHeight,answerNameHeight;
@synthesize delegate,indexPath;
//需要呼出自定义菜单
- (BOOL)canBecomeFirstResponder{
[super canBecomeFirstResponder];
return YES;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
// self.backgroundColor = [UIColor redColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
// self.answerContentLabel.backgroundColor = [UIColor redColor];
[self addSubview:self.headPicImgView];
[self addSubview:self.vipImg];
[self addSubview:self.answerNameLabel];
[self addSubview:self.hostLabel];
[self addSubview:self.timeLabel];
[self addSubview:self.answerContentLabel];
[self addSubview:self.expandBtn];
[self addSubview:self.lineView];
[self addSubview:self.praiseButton];
[self addSubview:self.menuButton];
[self addSubview:self.pushInfo];
[self addSubview:self.answerNameButton];
}
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]:@"";
answerNameHeight = heightForString(nameStr, self.answerNameLabel.font, 500, self.answerNameLabel.lineBreakMode);
// CGFloat trueContentHeight= returnTextHeightWithRTLabel(data.content, 1170/2, appFont(interactionFontSize, NO),10);
CGFloat trueContentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(data.content, 10, appFont(interactionFontSize, NO)), 1170/2, appFont(interactionFontSize, NO));
if (!data.isExpand || [data.isExpand intValue] == 0) {
answerContentHeight = trueContentHeight>interaction4LinesSpace?interaction4LinesSpace:trueContentHeight;
}else {
answerContentHeight = trueContentHeight;
}
NSString *praiseNum = data.praiseTimes;
dispatch_async(dispatch_get_main_queue(), ^{
self.headPicImgView.imageUrl = user.pic;
self.answerNameLabel.text = nameStr;
// self.answerContentLabel.text = data.content;
self.answerContentLabel.attributedText = getLineSpaceAttributedString(data.content, 10, appFont(interactionFontSize, NO));
if (trueContentHeight > interaction4LinesSpace) {
self.expandBtn.hidden = NO;
}else self.expandBtn.hidden = YES;
self.timeLabel.text = data.pubTime;
if (!isBlankString(praiseNum)) {
self.praiseButton.titleText = praiseNum;
}
if ([user.isAuth integerValue] ==1) {//认证用户提问或回答,头像不显示加”V“(bug:4116)
self.vipImg.hidden = NO;
}else{
self.vipImg.hidden = YES;
}
if (data.isPraised && [data.isPraised intValue] == 1) {
[self.praiseButton setSelect:YES animation:NO];
self.praiseButton.userInteractionEnabled = NO;
}else {
[self.praiseButton setSelect:NO animation:NO];
self.praiseButton.userInteractionEnabled = YES;
}
// width =widthForString(self.answerNameLabel.text, self.answerNameLabel.font, answerNameHeight, self.answerNameLabel.lineBreakMode);
[self layoutSubviews];
});
});
}
- (UIButton*)menuButton {
if (!_menuButton) {
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
_menuButton.backgroundColor = [UIColor clearColor];
[_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside];
}
return _menuButton;
}
- (AsyncImageView*)headPicImgView {
if (!_headPicImgView) {
_headPicImgView = [[AsyncImageView alloc]initWithFrame:CGRectZero];
_headPicImgView.layer.cornerRadius = 35.f/2;
_headPicImgView.clipsToBounds = YES;
_headPicImgView.isHaveWaterPrint = NO;
_headPicImgView.defaultImage = Image(@"setting/loginIcon_s.png");
}
return _headPicImgView;
}
-(UIImageView *)vipImg{//认证用户提问或回答,头像不显示加”V“(bug:4116)
if (!_vipImg) {
_vipImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
_vipImg.image = Image(@"setting/vipIcon.png");
_vipImg.hidden = YES;
}
return _vipImg;
}
-(UIButton *)pushInfo{
if (!_pushInfo) {
_pushInfo = [[UIButton alloc]init];
_pushInfo.backgroundColor = [UIColor clearColor];
[_pushInfo addTarget:self action:@selector(pushInSelector:) forControlEvents:UIControlEventTouchUpInside];
}
return _pushInfo;
}
- (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;
}
-(UIButton *)answerNameButton{
if (!_answerNameButton) {
_answerNameButton = [UIButton buttonWithType:UIButtonTypeCustom];
_answerNameButton.backgroundColor = [UIColor clearColor];
[_answerNameButton addTarget:self action:@selector(pushInSelector:) forControlEvents:UIControlEventTouchUpInside];
}
return _answerNameButton;
}
-(UIImageView *)hostLabel{//【需求】话题:话题的问答详情页(bug:4315)
if (!_hostLabel) {
_hostLabel = [[UIImageView alloc] initWithFrame:CGRectZero];
_hostLabel.image = Image(@"detailPage/tizhu.png");
// _hostLabel.text = @"题主";
// _hostLabel.textAlignment = NSTextAlignmentCenter;
// _hostLabel.backgroundColor = [UIColor colorWithHexString:@"0xec1c24"];
// _hostLabel.textColor = [UIColor whiteColor];
// _hostLabel.layer.cornerRadius = 20;
// _hostLabel.font = appFont(9, NO);
_hostLabel.hidden = YES;
}
return _hostLabel;
}
- (UILabel*)answerContentLabel {
if (!_answerContentLabel) {
_answerContentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_answerContentLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_answerContentLabel.textAlignment = RTTextAlignmentLeft;
_answerContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
_answerContentLabel.numberOfLines = 0;
_answerContentLabel.backgroundColor = [UIColor clearColor];
_answerContentLabel.font = appFont(interactionFontSize, NO);
}
return _answerContentLabel;
}
- (UILabel*)timeLabel {
if (!_timeLabel) {
_timeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_timeLabel.textColor = [UIColor colorWithHexString:LIGHTGRAY];
_timeLabel.font = appFont(11, NO);
_timeLabel.backgroundColor = [UIColor clearColor];
}
return _timeLabel;
}
- (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];
}
return _expandBtn;
}
- (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;
}
[self.praiseButton setSelected:YES animated:YES];//【需求】点赞动画(bug:4969)
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:@"24"];
NSString *praseNumStr = responseData[@"praiseTimes"];
if([praseNumStr intValue] <1000){//点赞:点赞超过1K的,几个页面会出现显示不全的现象(bug:5371)
Self.praiseButton.textLabel.text = praseNumStr;
}
Self.commentBO.isPraised = @"1";
Self.commentBO.praiseTimes = Self.praiseButton.textLabel.text;
if ([Self.delegate respondsToSelector:@selector(hasPraisedAnswer:withCommentBO:)]) {
[Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO];
}
TPLOG(@"点赞成功");
//点赞按钮:点赞按钮点赞后,再次点击异常(bug:5370)
Self.praiseButton.userInteractionEnabled = NO;
// [Self.praiseButton setSelect:YES animation:YES];
}else {
ShowMessage(@"点赞失败", NO);
}
}];
}
- (void)expandEvent:(UIButton*)btn {
if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) {
self.commentBO.isExpand = @"1";
}else {
self.commentBO.isExpand = @"0";
}
// CGFloat trueContentHeight= returnTextHeightWithRTLabel(self.commentBO.content, 1170/2, appFont(interactionFontSize, NO),10);
CGFloat trueContentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(self.commentBO.content, 10, appFont(interactionFontSize, NO)), 1170/2, appFont(interactionFontSize, NO));
if (expandCell) {
expandCell([self.commentBO.isExpand boolValue],trueContentHeight);
}
}
-(void) pushInSelector:(UIButton *) btn{
if ([self.delegate respondsToSelector:@selector(gotoUserInfo:)]) {
[self.delegate gotoUserInfo:self.commentBO];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat padding = 0;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
padding = 125;
}
self.headPicImgView.frame = CGRectMake(145/2+padding,
15,
35,
35);
self.vipImg.center = CGPointMake(self.headPicImgView.center.x+15, self.headPicImgView.center.y+12);//认证用户提问或回答,头像不显示加”V“(bug:4116)
self.answerNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headPicImgView.frame)+10,
CGRectGetMinY(self.headPicImgView.frame)+7,
500,
answerNameHeight);
CGFloat nameWidth = widthForString(self.answerNameLabel.text, self.answerNameLabel.font, answerNameHeight, self.answerNameLabel.lineBreakMode);
self.answerNameButton.frame = CGRectMake(CGRectGetMinX(self.answerNameLabel.frame)-5, CGRectGetMinY(self.answerNameLabel.frame)-5, nameWidth+10, answerNameHeight+10);
// self.hostLabel.frame = CGRectMake(CGRectGetMaxX(self.answerNameLabel.frame)+15,CGRectGetMinY(self.answerNameLabel.frame)+ CGRectGetHeight(self.answerNameLabel.frame)/2 - 6, 26, 12);
self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.answerNameLabel.frame),
CGRectGetMaxY(self.answerNameLabel.frame),
100,
16);
//247环境下,时事频道下的第一篇专题的最后一条热追问,点击展开箭头不可正常加载(BUG:4729)
// self.answerContentLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame),
// CGRectGetMaxY(self.headPicImgView.frame)+15,
// 1170/2,
// answerContentHeight);
self.answerContentLabel.frame = CGRectMake(CGRectGetMinX(self.headPicImgView.frame),
CGRectGetMaxY(self.headPicImgView.frame)+15,
1170/2,
answerContentHeight);
if (!self.commentBO.isExpand || [self.commentBO.isExpand intValue] == 0) {
[_expandBtn setImage:Image(@"detailPage/expandArrow.png") forState:UIControlStateNormal];
}else {
[_expandBtn setImage:Image(@"detailPage/expandArrowUp.png") forState:UIControlStateNormal];
}
self.expandBtn.frame = CGRectMake(CGRectGetMinX(self.answerContentLabel.frame),
CGRectGetMaxY(self.answerContentLabel.frame)+4,
CGRectGetWidth(self.answerContentLabel.frame),
30);
[self.expandBtn setImageEdgeInsets:UIEdgeInsetsMake(15-15/2,
CGRectGetWidth(self.expandBtn.frame)/2-13,
15-15/2,
CGRectGetWidth(self.expandBtn.frame)/2-13)];
self.lineView.frame = CGRectMake(CGRectGetMinX(self.answerContentLabel.frame),
CGRectGetHeight(self.bounds)-1,
CGRectGetWidth(self.answerContentLabel.frame),
1);
self.praiseButton.frame = CGRectMake(CGRectGetMaxX(self.lineView.frame)-40, CGRectGetMinY(self.headPicImgView.frame), 25, 45);
if(self.expandBtn.hidden || [self.commentBO.isExpand intValue] == 1){
self.menuButton.frame = self.answerContentLabel.frame;
}else{
self.menuButton.frame = CGRectMake(CGRectGetMinX(self.answerContentLabel.frame), CGRectGetMinY(self.answerContentLabel.frame), CGRectGetWidth(self.answerContentLabel.frame), interaction4LinesSpace);
}
self.pushInfo.frame = self.headPicImgView.frame;
}
- (void)expandCell:(void (^)(BOOL, CGFloat))block {
expandCell = [block copy];
}
#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:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu.menuVisible) {
[menu setMenuVisible:NO animated:YES];
return;
}
[menu setMenuItems:[NSArray arrayWithObjects: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(copyText:) || action == @selector(praise:)|| action == @selector(deleteComment:))
{
return YES;
}
else
{
return NO;
}
}
- (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;
}
[self.praiseButton setSelected:YES animated:YES];//【需求】点赞动画(bug:4969)
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:@"24"];
NSString *praseNumStr = responseData[@"praiseTimes"];
if([praseNumStr intValue] <1000){//点赞:点赞超过1K的,几个页面会出现显示不全的现象(bug:5371)
Self.praiseButton.textLabel.text = praseNumStr;
}
Self.commentBO.isPraised = @"1";
Self.commentBO.praiseTimes = Self.praiseButton.textLabel.text;
if ([Self.delegate respondsToSelector:@selector(hasPraisedAnswer:withCommentBO:)]) {
[Self.delegate hasPraisedAnswer:Self.indexPath withCommentBO:Self.commentBO];
}
TPLOG(@"点赞成功");
//点赞按钮:点赞按钮点赞后,再次点击异常(bug:5370)
Self.praiseButton.userInteractionEnabled = NO;
// [Self.praiseButton setSelect:YES animation:YES];
}else {
ShowMessage(@"点赞失败", NO);
}
}];
}
- (void)deleteComment:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"是否确认删除此发言?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != 0) {
NSDictionary *dic = @{@"commentIds":self.commentBO.commentId,@"commentType":@"1"};
__weak typeof(self) Self = self;
[Remote doJsonActionWithBlock:2001 requestUrl:removeCommentmspURL parameter:dic withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
[MobClick event:@"27"];
if ([Self.delegate respondsToSelector:@selector(deleteAnswerSuccess:)]) {
[Self.delegate deleteAnswerSuccess:self.indexPath];
}
ShowMessage(@"删除成功",YES);
}else {
ShowMessage(@"删除失败", NO);
}
}];
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
|