|
//
// commentCell.m
// ThePaperHD
//
// Created by scar1900 on 15/1/16.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "commentCell.h"
@interface commentCell() {
CGFloat contentHeight;
CGFloat nameHeight;
}
@end
@implementation commentCell
@synthesize commentBO = _commentBO;
@synthesize indexPath;
@synthesize delegate;
//需要呼出自定义菜单
- (BOOL)canBecomeFirstResponder{
[super canBecomeFirstResponder];
return YES;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self addSubview:self.headPicImgView];
[self addSubview:self.gotoInfo];
[self addSubview:self.lineView];
[self addSubview:self.commentNameLabel];
[self addSubview:self.commentNameBtn];
[self addSubview:self.timeLabel];
[self addSubview:self.commentContentLabel];
[self addSubview:self.praiseButton];
[self addSubview:self.smallIconView];
[self addSubview:self.menuButton];
}
return self;
}
- (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;
}
-(UIButton *)gotoInfo{
if (!_gotoInfo) {
_gotoInfo = [[UIButton alloc] init];
[_gotoInfo setBackgroundColor:[UIColor clearColor]];
[_gotoInfo addTarget:self action:@selector(gotoUser:) forControlEvents:UIControlEventTouchUpInside];
}
return _gotoInfo;
}
- (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;
}
- (UIView*)lineView {
if (!_lineView) {
_lineView = [[UIView alloc]initWithFrame:CGRectZero];
_lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _lineView;
}
- (UILabel*)commentNameLabel {
if (!_commentNameLabel) {
_commentNameLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_commentNameLabel.textColor = [UIColor colorWithHexString:BLUECOLOR];
_commentNameLabel.textAlignment = NSTextAlignmentLeft;
_commentNameLabel.font = appFont(15, NO);
_commentNameLabel.backgroundColor = [UIColor clearColor];
}
return _commentNameLabel;
}
-(UIButton *)commentNameBtn{
if (!_commentNameBtn) {
_commentNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_commentNameBtn.backgroundColor = [UIColor clearColor];
[_commentNameBtn addTarget:self action:@selector(gotoUser:) forControlEvents:UIControlEventTouchUpInside];
}
return _commentNameBtn;
}
- (UILabel*)commentContentLabel {
if (!_commentContentLabel) {
_commentContentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_commentContentLabel.textColor = [UIColor colorWithHexString:TextLightGray];
_commentContentLabel.numberOfLines = 0;
_commentContentLabel.textAlignment = NSTextAlignmentLeft;
_commentContentLabel.lineBreakMode = NSLineBreakByWordWrapping;
_commentContentLabel.font = appFont(interactionFontSize, NO);
_commentContentLabel.backgroundColor = [UIColor clearColor];
}
return _commentContentLabel;
}
- (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;
}
- (UIImageView*)smallIconView {
if (!_smallIconView) {
_smallIconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
_smallIconView.hidden = YES;
_smallIconView.image = Image(@"setting/vipIcon.png");
}
return _smallIconView;
}
-(void) gotoUser:(UIButton *) btn{
if ([self.delegate respondsToSelector:@selector(gotoUserInfo:)]) {
[self.delegate gotoUserInfo:self.commentBO];
}
}
- (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(hasPraisedComment:withCommentBO:)]) {
[Self.delegate hasPraisedComment:Self.indexPath withCommentBO:Self.commentBO];
}
TPLOG(@"点赞成功");
//点赞按钮:点赞按钮点赞后,再次点击异常(bug:5370)
Self.praiseButton.userInteractionEnabled = NO;
// [Self.praiseButton setSelect:YES animation:YES];
}else {
ShowMessage(@"点赞失败", NO);
}
}];
}
- (void)setCommentBO:(commentObjectVO *)commentData {
_commentBO = commentData;
if (_commentBO.quoteInfo.count >0) {
self.lineView.hidden = YES;
}else{
self.lineView.hidden = NO;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
userBO *user = setJsonDicToDataModel(commentData.userInfo, [userBO class]);
nameHeight = heightForString(user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@"", appFont(15, NO), 500, NSLineBreakByWordWrapping);
// contentHeight = returnTextHeightWithRTLabel(commentData.content, 930/2, appFont(interactionFontSize, NO), 10);
//修正1.6.8的RTLable相关Crash(bug:5925)
contentHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(commentData.content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO));
NSString *content = commentData.content;
NSString *praiseNum = commentData.praiseTimes;
NSString *userName = user.sname?[NSString stringWithFormat:@"%@:",user.sname]:@"";
// NSString *queName = @"";
// NSString *queContent = @"";
// if (commentData.quoteInfo && commentData.quoteInfo.allKeys.count>0) {
// NSDictionary *dic = commentData.quoteInfo;
// commentObjectVO *quoBO = setJsonDicToDataModel(dic, [commentObjectVO class]);
//
// queName =[NSString stringWithFormat:@"%@:",quoBO.userName];
// queContent = quoBO.content;
// }
dispatch_async(dispatch_get_main_queue(), ^{
self.commentNameLabel.text = userName;
//修正1.6.8的RTLable相关Crash(bug:5925)
if (!content || isBlankString(content)) {
self.commentContentLabel.text = @"";
}else{
self.commentContentLabel.attributedText = getLineSpaceAttributedString(content, 10, appFont(interactionFontSize, NO));
}
self.headPicImgView.imageUrl = user.pic;
if (!isBlankString(praiseNum)) {
self.praiseButton.titleText = praiseNum;
}
if (commentData.isPraised && [commentData.isPraised intValue] == 1) {
[self.praiseButton setSelect:YES animation:NO];
self.praiseButton.userInteractionEnabled = NO;
}else {
[self.praiseButton setSelect:NO animation:NO];
self.praiseButton.userInteractionEnabled = YES;
}
self.timeLabel.text = commentData.pubTime;
// if (!isBlankString(queName)) {
//
// self.commentBack.hidden = NO;
// self.quoNameLable.text =queName;
// self.quoContentLabel.text = queContent;
// }else {
// self.commentBack.hidden = YES;
// }
if ([user.isAuth intValue] == 1) {
self.smallIconView.hidden = NO;
}else self.smallIconView.hidden = YES;
[self layoutSubviews];
});
});
}
- (void)layoutSubviews {
self.headPicImgView.frame = CGRectMake(74/2, 15, 35, 35);
self.gotoInfo.frame = CGRectMake(74/2, 15, 35, 35);
self.smallIconView.center = CGPointMake(self.headPicImgView.center.x+15, self.headPicImgView.center.y+12);
CGFloat nameWidth = widthForString(self.commentNameLabel.text, self.commentNameLabel.font, nameHeight, self.commentNameLabel.lineBreakMode);
self.commentNameLabel.frame = CGRectMake(CGRectGetMaxX(self.headPicImgView.frame)+18,
CGRectGetMinY(self.headPicImgView.frame)+7,
400,
nameHeight);
self.commentNameBtn.frame = CGRectMake(CGRectGetMinX(self.commentNameLabel.frame)-5, CGRectGetMinY(self.commentNameLabel.frame), nameWidth+10, nameHeight+5);
self.commentContentLabel.frame = CGRectMake(CGRectGetMinX(self.commentNameLabel.frame),
CGRectGetMaxY(self.headPicImgView.frame)+15,
930/2,
contentHeight);
self.lineView.frame = CGRectMake(CGRectGetMinX(self.commentNameLabel.frame),
CGRectGetHeight(self.bounds)-1,
CGRectGetWidth(self.bounds),
1);
self.timeLabel.frame = CGRectMake(CGRectGetMinX(self.commentNameLabel.frame),
CGRectGetMaxY(self.commentNameLabel.frame),
100,
16);
self.praiseButton.frame = CGRectMake(CGRectGetMaxX(self.bounds)-40, CGRectGetMinY(self.headPicImgView.frame), 25, 45);
self.menuButton.frame = CGRectMake(CGRectGetMinX(self.commentContentLabel.frame),
CGRectGetMinY(self.commentContentLabel.frame),
930/2,
contentHeight);
}
#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 *delete = [[UIMenuItem alloc] initWithTitle:@"删除"action:@selector(deleteComment:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu.menuVisible) {
[menu setMenuVisible:NO animated:YES];
return;
}
NSDictionary *userInfo = self.commentBO.userInfo;
userBO *user = setJsonDicToDataModel(userInfo, [userBO class]);
if ([TPUserDefault instance].userBO
&& [user.userId longLongValue] == [[TPUserDefault instance].userBO.userId longLongValue]) {
[menu setMenuItems:[NSArray arrayWithObjects:delete,commentBack, copy, praise, nil]];
}else {
[menu setMenuItems:[NSArray arrayWithObjects:commentBack, copy, praise, nil]];
}
[menu setTargetRect:self.commentContentLabel.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:)
|| action == @selector(deleteComment:))
{
return YES;
}
else
{
return NO;
}
}
- (void)commentBack:(id)sender {
if ([self.delegate respondsToSelector:@selector(commentBackForUser:)]) {
[self.delegate commentBackForUser:self.commentBO];
}
}
- (void)copyText:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.commentContentLabel.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(hasPraisedComment:withCommentBO:)]) {
[Self.delegate hasPraisedComment: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(deleteCommentSuccess:)]) {
[Self.delegate deleteCommentSuccess:self.indexPath];
}
ShowMessage(@"删除成功",YES);
}else {
ShowMessage(@"删除失败", NO);
}
}];
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
|