// // HotAnswerTableViewVC.m // ThePaperBase // // Created by zhousan on 15/8/26. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "HotAnswerTableViewVC.h" #import "NewsForHotAnswerCell.h" #import "HotAskCell.h" #import "HotAnswerCell.h" #import "detailContentHomeController.h" #import "topicContentController.h" #import "askDetailController.h" #import "topicDeatilController.h" #import "myDynamicController.h" #import "HotAnswerContent.h" #import "HotAnswerIconCell.h" #import "AskContentAskCell.h" #import "HotAnswerAskIcon.h" #import "ImageShareController.h" @interface HotAnswerTableViewVC () @property(nonatomic, strong) NSMutableArray *commentList; @property(nonatomic, strong) NSMutableArray *dataList; @property(nonatomic, strong) NSMutableArray *heightList; @end @implementation HotAnswerTableViewVC - (void)viewDidLoad { [super viewDidLoad]; [self configCanRefresh:YES canLoad:YES]; [self firstAutoRefreshHandler]; self.tableView.clipsToBounds = YES; self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, CGRectGetHeight(self.view.frame)); self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; } - (void)needrefreshNightMode:(id)sender{ self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setCommentList:(NSMutableArray *)array { _commentList = [NSMutableArray array]; self.dataList = [NSMutableArray array]; self.heightList = [NSMutableArray array]; _commentList = array; [self.commentList enumerateObjectsUsingBlock:^(commentObjectVO *obj, NSUInteger idx, BOOL *stop) { NSDictionary *dic1 = @{@"comment":obj}; //原新闻 // NSArray *hotAnser = obj.answerList; //ask // NSDictionary *dic = hotAnser[0]; //answer // commentObjectVO *anserBO = setJsonDicToDataModel(dic, [commentObjectVO class]); [self.dataList addObject:dic1]; // [self.dataList addObject:obj]; // [self.dataList addObject:anserBO]; NSString *str = [NSString stringWithFormat:@"【原新闻】%@",obj.contName]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, rect_screen.size.width-40, 0)]; label.font = appFont(11, NO); label.lineBreakMode = NSLineBreakByCharWrapping; label.numberOfLines = 0; label.text = str; CGFloat newsHeight = [label sizeThatFits:CGSizeMake(rect_screen.size.width-40, CGFLOAT_MAX)].height+20; [self.heightList addObject:[NSString stringWithFormat:@"%f",newsHeight]]; NSDictionary *tempDic = setDataModelToDic(obj, [commentObjectVO class]); commentObjectVO *comment = setJsonDicToDataModel(tempDic, [commentObjectVO class]); comment.type = @"24"; [self.dataList addObject:comment]; [self.heightList addObject:@"50"];//提问头像 commentObjectVO *commentBO = setJsonDicToDataModel(tempDic, [commentObjectVO class]); commentBO.type = @"2"; CGFloat askHeight = 10; int LabelHeigth = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width-20-20 Text:commentBO.content LineSpace:10 ParagraphSpacing:0]; commentBO.labelHeight = LabelHeigth; [self.dataList addObject:commentBO]; askHeight += LabelHeigth; [self.heightList addObject:[NSString stringWithFormat:@"%f",askHeight]];//问正文高度 NSArray *hotAnser = obj.answerList; NSDictionary *dic = hotAnser[0]; commentObjectVO *anserBO = setJsonDicToDataModel(dic, [commentObjectVO class]); anserBO.type = @"34"; [self.dataList addObject:anserBO]; [self.heightList addObject:@"50"];//回答头像 commentObjectVO *answer = setJsonDicToDataModel(dic, [commentObjectVO class]); answer.type = @"3"; answer.askCommentBO = obj; if ([obj.answerNums intValue] > 3) { answer.hasMore = @"1"; } CGFloat anserHeight = 10; int anserH = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20-20 Text:answer.content LineSpace:10 ParagraphSpacing:0]; answer.labelHeight = anserH; [self.dataList addObject:answer]; if (anserH > getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)) { anserH = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE); anserHeight += 25; } anserHeight += anserH; [self.heightList addObject:[NSString stringWithFormat:@"%f",anserHeight+1]]; }]; [self.tableView reloadData]; } #pragma mark - TPTableView delegate - (void)pullRefreshHandler { [Remote doJsonAction:1 requestUrl:hotAnswerListURL parameter:nil delegate:self]; } - (void)pullLoadMoreHander { [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.heightList[indexPath.row] floatValue]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { id obj = self.dataList[indexPath.row]; if ([obj isKindOfClass:[commentObjectVO class]]) { commentObjectVO *commentBO = obj; switch ([commentBO.type integerValue]) { case 2://提问 return [self returnAskContentTableTable:table cellForRowAtIndexPath:indexPath]; break; case 3://回答 return [self returnAnswerContentTableTable:table cellForRowAtIndexPath:indexPath]; break; case 24://提问头像 return [self returnAskIconTable:table cellForRowAtIndexPath:indexPath]; break; case 34://回答头像 return [self returnAnswerIconTable:table cellForRowAtIndexPath:indexPath]; break; default: break; } }else if ([obj isKindOfClass:[NSDictionary class]]) { return [self returnNewsForHotCommentCellWithTableView:table cellForRowAtIndexPath:indexPath]; } return nil; } - (HotAnswerAskIcon *)returnAskIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index {//提问头像 static NSString *cellId = @"HotAnswerAskIcon"; HotAnswerAskIcon *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[HotAnswerAskIcon alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } cell.commentBO = self.dataList[index.row]; cell.delegate = self; return cell; } - (AskContentAskCell *)returnAskContentTableTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AskContentAskCell"; AskContentAskCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AskContentAskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } cell.commentBO = self.dataList[index.row]; cell.delegate = self; return cell; } - (HotAnswerIconCell *)returnAnswerIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerIconBaseCellId"; HotAnswerIconCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[HotAnswerIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } cell.commentBO = self.dataList[index.row]; cell.indexPath = index; cell.delegate = self; return cell; } - (HotAnswerContent *)returnAnswerContentTableTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerContentBaseCellId"; HotAnswerContent *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[HotAnswerContent alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } cell.shouldNoHiddenLine = @"1"; cell.commentBO = self.dataList[index.row]; cell.indexPath = index; cell.delegate = self; cell.commentNext = self.dataList[index.row - 2]; __block NSIndexPath *IndexPath = index; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { CGFloat backY = [cell.aswerContentLabel convertRect:cell.aswerContentLabel.bounds toView:KEY_WINDOW].origin.y-64; if (isExpand) { cellHeight = cellHeight+25+10; [Self.heightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; // NSIndexPath *lastIndex1 = [NSIndexPath indexPathForRow:IndexPath.row-2 inSection:IndexPath.section]; // NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:IndexPath.row-1 inSection:IndexPath.section]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; }else { cellHeight = getLineHeightWithCountAndFontWidth(4, 10, TEXT_FOUR_LEVELSIZE)+25+10; [Self.heightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; // NSIndexPath *lastIndex1 = [NSIndexPath indexPathForRow:IndexPath.row-2 inSection:IndexPath.section]; // NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:IndexPath.row-1 inSection:IndexPath.section]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; if (backY < 0) { [Self.tableView beginUpdates]; [Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; [Self.tableView endUpdates]; } } }]; return cell; } - (NewsForHotAnswerCell *)returnNewsForHotCommentCellWithTableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *NewsForHotAnswerCellId = @"NewsForHotAnswerCellId"; NewsForHotAnswerCell *cell = [table dequeueReusableCellWithIdentifier:NewsForHotAnswerCellId]; if (nil == cell) { cell = [[NewsForHotAnswerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsForHotAnswerCellId]; } NSDictionary *obj = self.dataList[indexPath.row]; cell.comment = obj[@"comment"]; return cell; } - (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath { id obj = self.dataList[indexPath.row]; if ([obj isKindOfClass:[commentObjectVO class]]) { }else{ commentObjectVO *comment = obj[@"comment"]; if ([comment.objectType isEqualToString:@"1"]) { detailContentHomeController *vc = detailContentHomeController.new; objInfoBO *objInfo = setJsonDicToDataModel(comment.objInfo, [objInfoBO class]); vc.nodeID = objInfo.forwordNodeId; [self.navigationController pushViewController:vc animated:YES]; addHaveReadFlag(objInfo.forwordNodeId); }else { topicContentController *topic = [topicContentController new]; TopicInfoBO *topicInfo = [[TopicInfoBO alloc] init]; topicInfo.topicId = comment.contId; topicInfo.praiseTimes = comment.praiseTimes; topic.preTopicInfo = topicInfo; [self.navigationController pushViewController:topic animated:YES]; } } } - (void)gotoCommentWithCommentBO:(commentObjectVO *)comment1 comment2:(commentObjectVO *)comment2 { if ([comment1.objectType isEqualToString:@"1"]) { askDetailController *hotAskVC = [[askDetailController alloc]init]; hotAskVC.commentBO = comment2; [self.navigationController pushViewController:hotAskVC animated:NO]; [hotAskVC gotoCommentFromHotAnwser:comment1]; }else { topicDeatilController *topic = [[topicDeatilController alloc] init]; topic.commentBO = comment2; topic.commentOT = @"3"; [self.navigationController pushViewController:topic animated:NO]; [topic gotoCommentFromHotAnwser:comment1]; } } #pragma mark - remote delegate - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)gotoUserInfo:(commentObjectVO *)comment { if (![comment.userInfo[@"userId"] isEqualToString:[TPUserDefault instance].userBO.userId]) { myDynamicController *dynamicVC = [myDynamicController new]; dynamicVC.comment = comment; [self.navigationController pushViewController:dynamicVC animated:YES]; }else{ } } - (void)askIconBaseGotoUserInfo:(commentObjectVO *)comment { [self gotoUserInfo:comment]; } - (void)askIconBaseGotoHotAsk:(commentObjectVO *)comment { [self gotoHotAsk:comment]; } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { NSMutableArray* commentList = [NSMutableArray array]; NSArray *list = responsData[@"qaList"]; if (actionTag == 1) { [self endRefresh]; }else { commentList = self.commentList; [self loadSuccess]; } self.nextUrl = responsData[@"nextUrl"]; [list enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [commentList addObject:commentBO]; }]; [self setCommentList:commentList]; } - (void)gotoHotAskWithCommentVO:(commentObjectVO *)comment { [self gotoHotAsk:comment]; } - (void)gotoHotAsk:(commentObjectVO *)comment { if ([comment.objectType isEqualToString:@"1"]) { askDetailController *hotAskVC = [[askDetailController alloc]init]; hotAskVC.commentBO = comment; [self.navigationController pushViewController:hotAskVC animated:YES]; }else { topicDeatilController *topic = [[topicDeatilController alloc] init]; TopicInfoBO *topicInfo = [TopicInfoBO new]; topicInfo.topicId = comment.contId; topic.topicInfo = topicInfo; // comment.type = @"2"; topic.commentBO = comment; topic.commentOT = @"3"; [self.navigationController pushViewController:topic animated:YES]; } } - (void)praiseHandlerWithIndexPath:(NSIndexPath *)indexPath { NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]; AnswerIconBaseCell *cell = (AnswerIconBaseCell *)[self.tableView cellForRowAtIndexPath:index]; [cell commentOK:cell.OkButton]; } #pragma mark --------- gotoshare -(void)gotoShare:(commentObjectVO *)comment index:(NSIndexPath *)index{ ImageShareController *imageShareController = [ImageShareController new]; imageShareController.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; commentObjectVO *askComment = comment.askCommentBO; CGFloat topicHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(comment.askCommentBO.contName, 3, appFont(TEXT_SIX_LEVELSIZE, NO)), 160, appFont(TEXT_SEVEN_LEVELSIZE, NO)); CGFloat askHeight = 5 + heightForAttributeStringWithLabel(getLineSpaceAttributedString(comment.askCommentBO.content, 6, appFont(TEXT_FIVE_LEVELSIZE, NO)), 170, appFont(TEXT_FIVE_LEVELSIZE, NO)); CGFloat answerHeight = 5 + heightForAttributeStringWithLabel(getLineSpaceAttributedString(comment.content, 6, appFont(TEXT_FIVE_LEVELSIZE, NO)), 170, appFont(TEXT_FIVE_LEVELSIZE, NO)); imageShareController.frame = CGRectMake(0, 0, 240, 340+topicHeight+askHeight+answerHeight); if ([comment.type intValue] == 3) { [imageShareController setAskData:askComment answerData:comment hostId:comment.askCommentBO.objInfo[@"userId"] topHeight:topicHeight askHeight:askHeight answerHeight:answerHeight]; }else{ [imageShareController setAskData:askComment answerData:comment hostId:nil topHeight:topicHeight askHeight:askHeight answerHeight:answerHeight]; } UIImage* img = [self capture:imageShareController]; if (!img) { ShowTextMessage(@"图片未完成"); return; } contentObjectBO *askContentBO = [contentObjectBO new]; askContentBO.name = comment.askCommentBO.contName; askContentBO.summary = comment.content; askContentBO.shareUrl = comment.askCommentBO.objInfo[@"shareUrl"]; if ([self.delegate respondsToSelector:@selector(hotAnswerTableToShare:contentBO:)]) { [self.delegate hotAnswerTableToShare:img contentBO:askContentBO]; } // if ([self.delegate respondsToSelector:@selector(askListToShare:contentBO:)]) { // [self.delegate askListToShare:img contentBO:askContentBO]; // } // } } - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo { NSString *msg = nil ; // if(error != NULL){ // msg = @"保存图片失败" ; // }else{ // msg = @"已保存至相册" ; // } ShowTextMessage(msg); } - (UIImage *)capture:(ImageShareController*)view { if ([view isKindOfClass:[UIImageView class]]) { return ((UIImageView*)view).image; } UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); [view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end