// // hotAskListController.m // ThePaperHD // // Created by scar1900 on 15/2/4. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "hotAskListController.h" #import "hotAskContentInListCell.h" #import "hotAnswerContentCell.h" #import "otherPersonHomepageController.h" #import "KGModal.h" #import "AnswerQuoBaseCell.h" @interface hotAskListController (){ NSMutableArray *dataArray; } @property(nonatomic, strong)UIView *headView; @property(nonatomic, strong)UIButton *headButton; @property(nonatomic, strong)UILabel *titleLabel; //@property(nonatomic, strong)UIButton *closeButton; @property(nonatomic, strong)NSString *commentId; @property(nonatomic, strong)NSMutableArray *heightArray; @property(nonatomic, strong)objInfoBO *contentInfo; @property(nonatomic, strong)UIButton *writeCommentBtn; @end @implementation hotAskListController @synthesize commentBO = _commentBO; @synthesize commentId; @synthesize qaDataSource = _qaDataSource,heightArray; @synthesize topicHostId = _topicHostId; @synthesize contentInfo; @synthesize delegate; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationController.navigationBarHidden = YES; self.tableView.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; [self.view addSubview:self.headView]; self.view.clipsToBounds = YES; UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.headView.bounds)-1, CGRectGetWidth(self.view.bounds), 1)]; lineView.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [self.headView addSubview:self.headButton]; [self.headView addSubview:lineView]; [self.headView addSubview:self.titleLabel]; // [self.headView addSubview:self.closeButton]; if([self.commentBO.objectType integerValue] ==3) { NSDictionary *dic = @{@"topicId":self.commentBO.contId}; [Remote doJsonAction:10001 requestUrl:topicInfoURL parameter:dic delegate:self]; } [self.view addSubview:self.writeCommentBtn]; [self configCanRefresh:YES canLoad:YES]; [self manualRefresh]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [KGModal sharedInstance].tapOutsideToDismiss = YES; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.headView.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-45); self.tableView.refreshControl.frame = CGRectMake(0, self.tableView.refreshControl.frame.origin.y, CGRectGetWidth(self.view.bounds), self.tableView.refreshControl.frame.size.height); self.tableView.refreshControl.originalContentInsectY = 0; [self setLoadFooterFrame:CGRectMake(0, CGRectGetHeight(self.tableView.bounds) ,CGRectGetWidth(self.tableView.bounds) , 60)]; self.headView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 45); self.titleLabel.frame = CGRectMake(CGRectGetWidth(self.headView.bounds)-116, 10, 100, 25); // self.closeButton.frame = CGRectMake(0, 0, 10+43/2+10, CGRectGetHeight(self.headView.bounds)); self.writeCommentBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-135/2, CGRectGetHeight(self.view.bounds)-100, 50, 50); //话题的问答详情页:通过如下方法进入话题的问答详情页显示异常(bug:6096) if (self.loadFooter) { self.tableView.tableFooterView = nil; self.tableView.tableFooterView = self.loadFooter; } } - (void)setCommentBO:(commentObjectVO *)data { _commentBO = data; self.tableView.tableFooterView.hidden = YES; if ([data.type intValue]== 2) { self.commentId = data.commentId; }else { if ([data.objectType integerValue] ==3) { self.commentId = data.parent[@"commentId"]; }else{ self.commentId = data.commentId; } } if([data.objectType isEqualToString:@"3"]){//【需求】问答详情页(bug:3921) self.titleLabel.text = @"进入原话题"; }else{ self.titleLabel.text = @"进入原新闻"; } } - (void)setQaDataSource:(NSMutableArray *)list { _qaDataSource = list; self.heightArray = [NSMutableArray array]; dataArray = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { if ([obj.type intValue] == 2) { CGFloat titleHeight = 50+returnTextHeightWithRTLabel(obj.content,930/2, appFont(interactionFontSize, NO),10)+15; //(姓名+内容高度+空行) [dataArray addObject:obj]; [self.heightArray addObject:[NSString stringWithFormat:@"%f",titleHeight]]; }else { NSMutableDictionary *dataDic = setDataModelToDic(obj, [commentObjectVO class]); commentObjectVO *answerBO = setJsonDicToDataModel(dataDic,[commentObjectVO class]); CGFloat contentHeight = 50+15; //提问者姓名+空行 CGFloat answerContent = heightForAttributeStringWithLabel(getLineSpaceAttributedString(answerBO.content, 10, appFont(interactionFontSize, NO)), 930/2, appFont(interactionFontSize, NO)); answerBO.labelHeight = answerContent; if (answerContent > interaction4LinesSpace) { //展开按钮高度25 answerContent = interaction4LinesSpace + 25; } contentHeight = contentHeight + answerContent+10; [dataArray addObject:answerBO]; [self.heightArray addObject:[NSString stringWithFormat:@"%f",contentHeight]]; //盖楼BO if (obj.quoteInfo.count>0 ) { commentObjectVO *quoComment = setJsonDicToDataModel(dataDic, [commentObjectVO class]); if (quoComment.quoteInfo && quoComment.quoteInfo.allKeys.count > 0) { commentObjectVO *quoInfo = setJsonDicToDataModel(quoComment.quoteInfo, [commentObjectVO class]); quoInfo.type = @"1000"; CGFloat quoHeight = 50/2+10+15+15; CGFloat quoLabelHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(quoInfo.content, 10, appFont(interactionFontSize, NO)), hotAskListPopUpSize.width-116, appFont(interactionFontSize, NO)); CGFloat twoLine = twoLineheight; quoInfo.labelHeight = quoLabelHeight; if (quoLabelHeight > twoLine) { quoLabelHeight = twoLine; quoHeight += 15; } quoHeight += quoLabelHeight; [dataArray addObject:quoInfo]; [self.heightArray addObject:[NSString stringWithFormat:@"%f",quoHeight]]; } } } }]; [self.tableView reloadData]; } -(void)setTopicHostId:(NSString *)hostId{ _topicHostId = hostId; } - (UIView *)headView { if (!_headView) { _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 45)]; _headView.backgroundColor = [UIColor colorWithHexString:CardBackGroundColor]; } return _headView; } - (UILabel*)titleLabel { if (!_titleLabel) { // _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.headView.bounds)-110, 10, 100, 30)]; _titleLabel = [[UILabel alloc] init]; // _titleLabel.text = @"进入原新闻"; _titleLabel.font = appFont(15, NO); _titleLabel.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor; _titleLabel.layer.borderWidth = 1; _titleLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.backgroundColor = [UIColor clearColor]; } return _titleLabel; } //- (UIButton*)closeButton { // if (!_closeButton) { // _closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; // _closeButton.frame = CGRectMake(0, 0, 10+43/2+10, CGRectGetHeight(self.headView.bounds)); // [_closeButton setImage:Image(@"Button/closeBlueBtn.png") forState:UIControlStateNormal]; // [_closeButton addTarget:self action:@selector(closeViewHandler:) forControlEvents:UIControlEventTouchUpInside]; // } // return _closeButton; //} //- (void)closeViewHandler:(UIButton*)btn { // if ([self.delegate respondsToSelector:@selector(clickClose)]) { // [self.delegate clickClose]; // } //} - (UIButton*)headButton { if (!_headButton) { _headButton = [UIButton buttonWithType:UIButtonTypeCustom]; _headButton.backgroundColor = [UIColor clearColor]; _headButton.frame = CGRectMake(0, 0, CGRectGetWidth(self.headView.bounds), CGRectGetHeight(self.headView.bounds)); [_headButton addTarget:self action:@selector(clickHead:) forControlEvents:UIControlEventTouchUpInside]; [_headButton setBackgroundImage:imageWithUIColor([UIColor clearColor]) forState:UIControlStateNormal]; [_headButton setBackgroundImage:imageWithUIColor([UIColor colorWithHexString:LINECOLOR]) forState:UIControlStateHighlighted]; } return _headButton; } - (void)clickHead:(UIButton*)btn { if(![self.commentBO.objectType isEqualToString:@"3"]){//【需求】问答详情页(bug:3921) if (self.contentInfo) { if ([self.delegate respondsToSelector:@selector(goToOriginContent:presentedController:)]) { [self.delegate goToOriginContent:self.contentInfo presentedController:self]; } } }else{ TopicInfoBO *topic = [[TopicInfoBO alloc]init]; topic.topicId = self.commentBO.contId; if (self.qaDataSource.count >1) {// 他人主页,进入一个没有题主回答过的话题问答详情页,点击进入原话题,必定闪退(bug:4297) commentObjectVO *firstComment = self.qaDataSource[1]; topic.userInfo = firstComment.userInfo; } if ([self.delegate respondsToSelector:@selector(hotAskToTopicInfo:user:)]) { [self.delegate hotAskToTopicInfo:topic user:setJsonDicToDataModel(topic.userInfo, [userBO class])]; } } } - (UIButton*)writeCommentBtn { if (!_writeCommentBtn) { _writeCommentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; if ([[TPUserDefault instance].isNightMode intValue] ==1) {//夜间模式下,进入新闻详情页,左划进入到追问页,评论按钮底色不对(bug:4806) [_writeCommentBtn setImage:Image(@"Button/writeComment_night.png") forState:UIControlStateNormal]; }else{ [_writeCommentBtn setImage:Image(@"Button/writeComment.png") forState:UIControlStateNormal]; } [_writeCommentBtn setImage:Image(@"Button/writeComment_h.png") forState:UIControlStateHighlighted]; [_writeCommentBtn addTarget:self action:@selector(writeComment:) forControlEvents:UIControlEventTouchUpInside]; } return _writeCommentBtn; } - (void)writeComment:(UIButton*)btn { if ([self.delegate respondsToSelector:@selector(goWriteComment)]) { [self.delegate goWriteComment]; } } #pragma mark - refresh and load more - (void)remoteMethod:(NSString*)commentIdStr { NSDictionary *dic = @{@"commentId":commentIdStr}; [Remote doJsonAction:1 requestUrl:singleAskDetailListURL parameter:dic delegate:self]; } - (void)pullRefreshHandler { // if (self.commentId &&self.commentId.length>0) { // [self remoteMethod:self.commentId]; // }else{ // [self remoteMethod:self.commentBO.parent[@"commentId"]]; // } if (![self.commentBO.type isEqualToString:@"3"]) { [self remoteMethod:self.commentId]; }else{ [self remoteMethod:self.commentBO.parent[@"commentId"]]; } } - (void)pullLoadMoreHander { [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } #pragma mark - tableview delegate and dataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataArray.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (heightArray.count > 0) { // TPLOG(@"-------------->%f",[heightArray[indexPath.row] floatValue]); return [heightArray[indexPath.row] floatValue]; }else return 0.f; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { commentObjectVO* commentObj =dataArray[indexPath.row]; // if ([commentObj.type intValue] == 2) { //提问 if(commentObj.answerNums && commentObj.answerNums >0){ return [self getAskContentCell:table cellForRowAtIndexPath:indexPath]; }else { //回答 if ([commentObj.type intValue] == 1000) { return [self getQuoCell:table cellForRowAtIndexPath:indexPath]; }else{ return [self getAnserContentCell:table cellForRowAtIndexPath:indexPath]; } } } //回答详情 - (hotAnswerContentCell*)getAnserContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"answerContentCell"; hotAnswerContentCell *cell = (hotAnswerContentCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[hotAnswerContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.commentBO = dataArray[indexPath.row]; cell.delegate = self; cell.indexPath = indexPath; cell.topicHostId = _topicHostId; __block NSIndexPath* IndexPath = indexPath; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { if (isExpand) { CGFloat offset = cellHeight - 100; NSString *qaAllHeightStr = Self.heightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]+offset; [Self.heightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",qaAllHeight]]; NSArray *array = @[IndexPath]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic]; }else { CGFloat offset = cellHeight - 100; NSString *qaAllHeightStr = Self.heightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]-offset; [Self.heightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",qaAllHeight]]; NSArray *array = @[IndexPath]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic]; [Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } }]; return cell; } - (AnswerQuoBaseCell *)getQuoCell:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *AnswerQuoBaseCellID = @"AnswerQuoBaseCell"; AnswerQuoBaseCell* cell = (AnswerQuoBaseCell*)[table dequeueReusableCellWithIdentifier:AnswerQuoBaseCellID]; if (nil == cell) { cell = [[AnswerQuoBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AnswerQuoBaseCellID]; } commentObjectVO *commentBO = dataArray[indexPath.row]; cell.isMe = NO; cell.commentBO = commentBO; cell.delegate = self; __block NSIndexPath *IndexPath = indexPath; __weak typeof(self) Self = self; [cell quoBlock:^(BOOL isQuoExpand, CGFloat quoHeight) { CGFloat backY = [cell.quoContentLabel convertRect:cell.quoContentLabel.bounds toView:KEY_WINDOW].origin.y-170; if (isQuoExpand) { quoHeight = quoHeight+50/2+10+15+15; [self.heightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; }else { CGFloat two = (appFont(TEXT_FOUR_LEVELSIZE, NO).lineHeight+10)*2; quoHeight = two+50/2+10+15+15+15; [self.heightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]]; 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; } //问题内容 - (hotAskContentInListCell*)getAskContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"askContentCell"; hotAskContentInListCell *cell = (hotAskContentInListCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[hotAskContentInListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.isGrayBack = YES; cell.isHaveMenu = YES; cell.hotAskDelegate = self; cell.askContentDelegate = self; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (void)hasPraisedAnswer:(NSIndexPath *)indexPath withCommentBO:(commentObjectVO *)commentBO{ [self.qaDataSource replaceObjectAtIndex:indexPath.row withObject:commentBO]; } //- (void)menuClickAnswer:(commentObjectVO *)commentBO { // if ([self.delegate respondsToSelector:@selector(goWriteComment)]) { // [self.delegate goWriteComment]; // } //} -(void)menuClickAnswer:(commentObjectVO *)commentBO closeHidden:(BOOL)isHidden{ if ([self.delegate respondsToSelector:@selector(goWriteComment)]) { [self.delegate goWriteComment]; } } - (void)deleteAnswerSuccess:(NSIndexPath *)indexPath { [self manualRefresh]; } - (void)deleteAskSuccess { [self manualRefresh]; } - (void)focusAsk:(commentObjectVO *)comment isToFocus:(BOOL)isToFocus button:(UIButton *)btn { if ([self.delegate respondsToSelector:@selector(focusCurrentAsk:isToFocus:button:)]) { [self.delegate focusCurrentAsk:comment isToFocus:isToFocus button:btn]; } } -(void)gotoUserInfo:(commentObjectVO *)comment{ if ([self.delegate respondsToSelector:@selector(gotoParent:)]) { [self.delegate gotoParent:comment]; } } #pragma mark #pragma mark - remote delegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (10001 == actionTag) { TopicInfoBO *topic = setJsonDicToDataModel(responsData[@"topicInfo"], [TopicInfoBO class]); userBO *user = setJsonDicToDataModel(topic.userInfo, [userBO class]); // self.hotAskVC.topicHostId =user.userId; self.topicHostId = user.userId; }else if (actionTag == 1) { if (responsData) { self.qaDataSource = [NSMutableArray array]; NSString *answerNums = responsData[@"recordTotal"]; if (responsData[@"objInfo"]) { objInfoBO *infoBO = setJsonDicToDataModel(responsData[@"objInfo"], [objInfoBO class]); self.contentInfo = infoBO; } NSMutableArray *qaList = [NSMutableArray array]; if (responsData[@"question"]) { commentObjectVO *questionObj = setJsonDicToDataModel(responsData[@"question"], [commentObjectVO class]); //加入提问 questionObj.answerNums = answerNums; [qaList addObject:questionObj]; } if (responsData[@"answerList"]) { NSArray *answerList = responsData[@"answerList"]; if (answerList.count > 0) { [answerList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commentObjectVO *answerobj = setJsonDicToDataModel(obj, [commentObjectVO class]); [qaList addObject:answerobj]; }]; } } self.qaDataSource = qaList; [self endRefresh]; self.tableView.tableFooterView.hidden = NO; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } }else { NSMutableArray *qaList = [NSMutableArray arrayWithArray:self.qaDataSource]; if (responsData[@"answerList"]) { NSArray *answerList = responsData[@"answerList"]; if (answerList.count > 0) { [answerList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commentObjectVO *answerobj = setJsonDicToDataModel(obj, [commentObjectVO class]); [qaList addObject:answerobj]; }]; } } self.qaDataSource = [NSMutableArray arrayWithArray:qaList]; [self loadSuccess]; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } } - (void)answerForUserBack:(commentObjectVO *)commentBO closeHidden:(BOOL)isHidden{ if([self.delegate respondsToSelector:@selector(answerBackUser: closeHidden:)]) { [self.delegate answerBackUser:commentBO closeHidden:YES]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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