|
//
// otherPersonCell.m
// ThePaperHD
//
// Created by YoungLee on 15/4/27.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "otherPersonCell.h"
#import "TPSelectButton.h"
#import "commentOkButton.h"
@interface otherPersonCell(){
CGFloat titleHeight;
CGFloat contentHeight;
}
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)UIImageView *typeImg;
@property(nonatomic, strong)UILabel *typeLabel;
@property(nonatomic, strong)RTLabel *title;
@property(nonatomic, strong)RTLabel *content;
@property(nonatomic, strong)UIImageView *timeIcon;
@property(nonatomic, strong)UILabel *time;
@property(nonatomic, strong)commentOkButton *praiseButton;
@property(nonatomic, strong)UILabel *praiseText;
@property(nonatomic, strong)UIView *line;
@property(nonatomic, strong)UIButton *pushDetail;
@property(nonatomic, strong)UIButton *menuButton;
@property(nonatomic, strong)UIButton *gotoAskInfo;
@property(nonatomic, strong)id otherData;
@end
@implementation otherPersonCell
@synthesize otherData = _otherData;
@synthesize indexPath;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
//需要呼出自定义菜单
- (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.clipsToBounds = YES;
self.contentView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR];
[self.praiseButton changeButtonImage];
[self.contentView addSubview:self.backView];
[self.contentView addSubview:self.typeImg];
[self.typeImg addSubview:self.typeLabel];
[self.backView addSubview:self.title];
[self.backView addSubview:self.content];
[self.backView addSubview:self.timeIcon];
[self.backView addSubview:self.time];
[self.backView addSubview:self.praiseButton];
[self.backView addSubview:self.praiseText];
[self.backView addSubview:self.line];
[self.backView addSubview:self.gotoAskInfo];
[self.backView addSubview:self.pushDetail];
[self.backView addSubview:self.menuButton];
[self layoutSub];
}
return self;
}
-(void)setData:(id)dataSource{
_otherData = dataSource;
if ([dataSource isKindOfClass:[commentObjectVO class]]) {
commentObjectVO *commentBo = dataSource;
self.title.hidden = NO;
self.typeLabel.text = [self setTypeName:commentBo.type];
self.title.text = [self settitleName:commentBo];
self.content.text = commentBo.content;
self.time.text = commentBo.pubTime;
self.praiseButton.text = commentBo.praiseTimes;
if ([commentBo.isPraised isEqualToString:@"1"]) {
self.praiseButton.selected = YES;
}else{
self.praiseButton.selected = NO;
}
}else if([dataSource isKindOfClass:[TopicInfoBO class]]){
TopicInfoBO *topicInfo = dataSource;
self.typeLabel.text = @"话题";
self.content.text = topicInfo.title;
self.time.text = topicInfo.publishTime;
self.praiseButton.text = topicInfo.praiseTimes;
titleHeight = 0;
self.title.hidden = YES;
if ([topicInfo.isPraised isEqualToString:@"1"]) {
self.praiseButton.selected = YES;
}else{
self.praiseButton.selected = NO;
}
}
// titleHeight =6+ [self.title sizeThatFits:CGSizeMake(rect_screen.size.width-40, NO)].height;
titleHeight = returnTextHeightWithRTLabel(self.title.text, rect_screen.size.width- 43, self.title.font, self.title.lineSpacing);
// contentHeight = [self.content sizeThatFits:CGSizeMake(rect_screen.size.width-40, 0)].height;
contentHeight = returnTextHeightWithRTLabel(self.content.text, rect_screen.size.width-40, self.content.font, self.content.lineSpacing);
[self relayout];
}
//1-跟帖,2-提问,3-回答
-(NSString *) setTypeName:(NSString *) typeStr{
NSString *str;
switch ([typeStr integerValue]) {
case 1:
str = @"评";
self.praiseText.hidden = NO;
self.praiseButton.hidden = NO;
self.menuButton.hidden = NO;
self.gotoAskInfo.hidden = YES;
break;
case 2:
str = @"问";
self.praiseText.hidden = YES;
self.praiseButton.hidden = YES;
self.menuButton.hidden = YES;
self.gotoAskInfo.hidden = NO;
break;
case 3:
str = @"答";
self.praiseText.hidden = NO;
self.praiseButton.hidden = NO;
self.menuButton.hidden = NO;
self.gotoAskInfo.hidden = YES;
break;
default:
break;
}
return str;
}
//1- 新闻 2- 直播栏目 3- 话题
-(NSString*) settitleName:(commentObjectVO *) comment{
NSString *str;
NSString *contName = comment.contName;
NSString *contentName = comment.parent[@"content"];
if([comment.type isEqualToString:@"1"]){//如果是评论,后面只能是原新闻
str = [NSString stringWithFormat:@"【原新闻】%@",contName];
}else if ([comment.type isEqualToString:@"2"]){//如果是提问,后面只有两种:原话题,原新闻
if([comment.objectType isEqualToString:@"3"]){
str = [NSString stringWithFormat:@"【原话题】%@",contName];
}else{
str = [NSString stringWithFormat:@"【原新闻】%@",contName];
}
}else{
str = [NSString stringWithFormat:@"【原提问】%@",contentName];
}
return str;
}
#pragma mark -- view
-(UIView *)backView{
if (!_backView) {
_backView = [[UIView alloc] init];
_backView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
}
return _backView;
}
-(UIImageView *)typeImg{
if (!_typeImg) {
_typeImg = [UIImageView new];
_typeImg.image = Image(@"topic/otherType.png");
}
return _typeImg;
}
-(UILabel *)typeLabel{
if (!_typeLabel) {
_typeLabel = [[UILabel alloc] init];
_typeLabel.backgroundColor = [UIColor clearColor];
_typeLabel.textColor = [UIColor whiteColor];
_typeLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO);
_typeLabel.textAlignment = NSTextAlignmentCenter;
}
return _typeLabel;
}
-(RTLabel *)title{
if (!_title) {
_title = [[RTLabel alloc] init];
_title.font = appFont(TEXT_SIX_LEVELSIZE, NO);
// _title.numberOfLines = 0;
_title.lineSpacing = 2;
_title.lineBreakMode = RTTextLineBreakModeWordWrapping;
_title.textColor = [UIColor colorWithHexString:LIGHTGRAY];
}
return _title;
}
-(RTLabel *)content{
if (!_content) {
_content = [[RTLabel alloc] init];
_content.font = appFont(TEXT_FOUR_LEVELSIZE, NO);
// _content.numberOfLines = 0;
// _content.lineBreakMode = NSLineBreakByWordWrapping;
_content.lineSpacing = 7;
_content.lineBreakMode = RTTextLineBreakModeWordWrapping;
_content.textColor = [UIColor colorWithHexString:TextBlack];
_content.backgroundColor = [UIColor clearColor];
}
return _content;
}
-(UIImageView *)timeIcon{
if (!_timeIcon) {
_timeIcon = [[UIImageView alloc] initWithFrame:CGRectZero];
_timeIcon.image = Image(@"topic/timeImg.png");
}
return _timeIcon;
}
-(UILabel *)time{
if (!_time) {
_time = [[UILabel alloc]init];
_time.font = appFont(TEXT_SEVEN_LEVELSIZE, NO);
_time.textColor = [UIColor colorWithHexString:LIGHTGRAY];
_time.backgroundColor = [UIColor clearColor];
}
return _time;
}
-(UIButton *)gotoAskInfo{
if (!_gotoAskInfo) {
_gotoAskInfo = [UIButton buttonWithType:UIButtonTypeCustom];
_gotoAskInfo.backgroundColor = [UIColor clearColor];
_gotoAskInfo.hidden = YES;
[_gotoAskInfo addTarget:self action:@selector(gotoAskInfo:) forControlEvents:UIControlEventTouchUpInside];
}
return _gotoAskInfo;
}
-(UIButton *)pushDetail{
if (!_pushDetail) {
_pushDetail = [[UIButton alloc] init];
_pushDetail.backgroundColor = [UIColor clearColor];
[_pushDetail addTarget:self action:@selector(pushDetailSelector:) forControlEvents:UIControlEventTouchUpInside];
}
return _pushDetail;
}
- (commentOkButton*)praiseButton {
if (!_praiseButton) {
_praiseButton = [commentOkButton buttonWithType:UIButtonTypeCustom];
[_praiseButton addTarget:self action:@selector(commentOK:) forControlEvents:UIControlEventTouchUpInside];
}
return _praiseButton;
}
-(UIView *)line{
if (!_line) {
_line = [[UIView alloc]init];
_line.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
}
return _line;
}
- (UIButton*)menuButton {
if (!_menuButton) {
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
_menuButton.backgroundColor = [UIColor clearColor];
[_menuButton addTarget:self action:@selector(menuTap:) forControlEvents:UIControlEventTouchUpInside];
}
return _menuButton;
}
#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(commentOK:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu.menuVisible) {
[menu setMenuVisible:NO animated:YES];
return;
}
[menu setMenuItems:[NSArray arrayWithObjects:commentBack, copy, praise, nil]];
[menu setTargetRect:self.content.frame inView:self.backView];
[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(commentOK:))
{
return YES;
}
else
{
return NO;
}
}
-(void) pushDetailSelector:(UIButton*)btn{
//个人主页中新闻追问的回答进入原提问,不显示原题问和其他回答(bug:5298)
if ([self.typeLabel.text isEqualToString:@"答"]) {
commentObjectVO *comment = (commentObjectVO*)_otherData;
comment.commentId = comment.parent[@"commentId"];
if ([self.otherDelegate respondsToSelector:@selector(pushToAskInfo:)]) {
[self.otherDelegate pushToAskInfo:comment];
}
}else{
if ([self.otherDelegate respondsToSelector:@selector(pushToDetail:)]) {
[self.otherDelegate pushToDetail:_otherData];
}
}
}
-(void) gotoAskInfo:(UIButton*) btn{
if ([self.otherDelegate respondsToSelector:@selector(pushToAskInfo:)]) {
[self.otherDelegate pushToAskInfo:(commentObjectVO*)_otherData];
}
}
- (void)commentBack:(id)sender {
if ([self.otherDelegate respondsToSelector:@selector(commentBackForUser:)]) {
[self.otherDelegate commentBackForUser:(commentObjectVO *)_otherData];
}
}
- (void)copyText:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.string = self.content.text;
ShowMessage(@"复制成功", YES);
}
- (void)commentOK:(UIButton*)btn {
if (self.praiseButton.isSelected) {
return;
}
[self.praiseButton setSelected:YES animated:YES];
// NSDictionary *dic = @{@"topicId":topic.topicId,@"otype":@3};
__weak typeof(self) Self = self;
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
if ([_otherData isKindOfClass:[commentObjectVO class]]) {
commentObjectVO *comment = (commentObjectVO*)_otherData;
[dic setObject:comment.commentId forKey:@"commentId"];
[Remote doJsonActionWithBlock:2000 requestUrl:commentPraiseURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSString *praseNumStr = responseData[@"praiseTimes"];
if ([praseNumStr intValue] <1000) {// 个人动态,对一个创建的话题点赞,超过千位数的,会显示…(bug:5187)
Self.praiseButton.text = praseNumStr;
}
// commentObjectVO *comment = (commentObjectVO*)_otherData;
comment.isPraised = @"1";
//点赞数:当点赞数达到1K以上时,点赞后点赞数异常(bug:5372)
comment.praiseTimes = Self.praiseButton.text;
if ([self.otherDelegate respondsToSelector:@selector(hasPraisedComment:withCommentBO:)]) {
[self.otherDelegate hasPraisedComment:indexPath withCommentBO:comment];
}
TPLOG(@"点赞成功");
}else {
TPLOG(@"点赞失败");
}
}];
}else if ([_otherData isKindOfClass:[TopicInfoBO class]]){
TopicInfoBO *topic = (TopicInfoBO *)_otherData;
[dic setObject:topic.topicId forKey:@"topicId"];
[dic setObject:@3 forKey:@"otype"];
[Remote doJsonActionWithBlock:2000 requestUrl:optTopicURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) {
if (success) {
NSString *praseNumStr = responseData[@"praiseTimes"];
// if (![praseNumStr containsString:@"k"] || ![praseNumStr containsString:@"m"]) {
if ([praseNumStr intValue] <1000) {
Self.praiseButton.text = praseNumStr;
}
// Self.praiseButton.text = praseNumStr;
// TopicInfoBO *topic = (TopicInfoBO *)_otherData;
topic.isPraised = @"1";
topic.praiseTimes = Self.praiseButton.text;
TPLOG(@"点赞成功");
if ([self.otherDelegate respondsToSelector:@selector(hasPraisedTopic:withCommentBO:)]) {
[self.otherDelegate hasPraisedTopic:indexPath withCommentBO:topic];
}
}else {
TPLOG(@"点赞失败");
}
}];
}
}
-(void)layoutSub{
[self.backView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.contentView.left).offset(10);
make.right.equalTo(self.contentView.right).offset(-10);
make.top.equalTo(self.contentView.top).offset(10);
make.bottom.equalTo(self.contentView.bottom);
}];
[self.pushDetail makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left);
make.right.equalTo(self.backView.right);
make.top.equalTo(self.title.top);
make.bottom.equalTo(self.title.bottom);
}];
[self.typeImg makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.backView.right).offset(5);
make.width.equalTo(@25);
make.top.equalTo(self.backView.top).offset(-5);
make.height.equalTo(@25);
}];
[self.typeLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.typeImg.left).offset(5);
make.right.equalTo(self.typeImg.right);
make.top.equalTo(self.typeImg.top);
make.bottom.equalTo(self.typeImg.bottom).offset(-5);
}];
[self.line makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left);
make.right.equalTo(self.backView.right);
make.top.equalTo(self.title.bottom).offset(4);
make.height.equalTo(@0.5);
}];
// [self.content makeConstraints:^(MASConstraintMaker *make) {
// make.left.equalTo(self.backView.left).equalTo(10);
// make.right.equalTo(self.backView.right).equalTo(-10);
// make.top.equalTo(self.title.bottom).offset(5);
// make.height.mas_equalTo(25);
// }];
[self.gotoAskInfo makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left);
make.right.equalTo(self.backView.right);
make.top.equalTo(self.content.top).offset(-5);
make.bottom.equalTo(self.content.bottom).offset(10);
}];
[self.menuButton makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left);
make.right.equalTo(self.backView.right);
make.top.equalTo(self.content.top).offset(-5);
make.bottom.equalTo(self.content.bottom).offset(10);
}];
[self.timeIcon makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.content.left);
make.width.equalTo(@10);
make.top.equalTo(self.content.bottom).offset(13);
make.height.equalTo(@10);
}];
[self.time makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.timeIcon.right).offset(5);
make.width.mas_equalTo(@100);
make.top.equalTo(self.timeIcon.top);
make.height.equalTo(@10);
}];
[self.praiseButton makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.right).offset(-50);
make.width.equalTo(@50);
make.top.equalTo(self.time.top).offset(-5);
make.height.equalTo(@35);
}];
}
-(void) relayout{
[self.title remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left).offset(3);
make.right.equalTo(self.backView.right).offset(-20);
make.top.equalTo(self.backView.top).offset(4);
make.height.mas_equalTo(titleHeight);
}];
[self.content remakeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.backView.left).equalTo(10);
make.right.equalTo(self.backView.right).equalTo(-10);
make.top.equalTo(self.line.bottom).offset(10);
make.height.mas_equalTo(contentHeight);
}];
}
@end
|