// // myAsksController.m // ThePaperHD // // Created by scar1900 on 15/3/30. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "myAsksController.h" @interface myAsksController () @end @implementation myAsksController @synthesize qaList = _qaList; @synthesize qaDataSource; @synthesize qaHeightArray; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:self.noDataBack]; self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); self.tableView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.tableView.separatorStyle = UITableViewCellSelectionStyleNone; // Do any additional setup after loading the view. [self configCanRefresh:YES canLoad:YES]; self.tableView.refreshControl.originalContentInsectY = 0; [self manualRefresh]; } - (UIView*)noDataBack { if (!_noDataBack) { _noDataBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, myAskCenterPopSize.width, CGRectGetHeight(self.view.bounds))]; _noDataBack.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; _noDataBack.hidden = YES; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(80, 350/2, 780/2, 20)]; label.textColor = [UIColor colorWithHexString:TextGray]; label.font = appFont(20, NO); label.textAlignment = NSTextAlignmentLeft; label.text = @"看完新闻还有问题没搞懂?去提个问题吧。"; [_noDataBack addSubview:label]; } return _noDataBack; } - (void)setQaList:(NSMutableArray *)list { _qaList = list; if ([self checkIfNoData:list]) { return; } self.qaDataSource = [NSMutableArray array]; self.qaHeightArray = [NSMutableArray array]; [self.qaList enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { [self.qaDataSource addObject:obj]; CGFloat titleHeight = 50+returnTextHeightWithRTLabel(obj.content,930/2, appFont(interactionFontSize, NO),10)+15; //(姓名+内容高度+空行) [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",titleHeight]]; NSArray *qaArray = obj.answerList; [qaArray enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger index, BOOL *isStop) { commentObjectVO *anserBO = setJsonDicToDataModel(dic, [commentObjectVO class]); [self.qaDataSource addObject:anserBO]; CGFloat contentHeight = 50+15; //提问者姓名+空行 CGFloat answerContent = returnTextHeightWithRTLabel(anserBO.content, 930/2, appFont(interactionFontSize, NO),10)>interaction4LinesSpace?interaction4LinesSpace+30:returnTextHeightWithRTLabel(anserBO.content, 930/2, appFont(interactionFontSize, NO),10); contentHeight = contentHeight + answerContent+10; if (anserBO.quoteInfo && anserBO.quoteInfo.allKeys.count>0) {//假如引用信息(盖楼) contentHeight = contentHeight + 190/2; } [self.qaHeightArray addObject:[NSString stringWithFormat:@"%f",contentHeight]]; }]; }]; [self.tableView reloadData]; } - (BOOL)checkIfNoData:(NSArray*)list { if (list.count > 0) { self.tableView.hidden =NO; self.noDataBack.hidden = YES; return NO; }else { self.tableView.hidden =YES; self.noDataBack.hidden =NO; return YES; } } #pragma mark - refresh and load handler - (void)pullRefreshHandler { NSDictionary *dic = @{@"ctype":@(2)}; [Remote doJsonAction:1 requestUrl:myQaListURL parameter:dic delegate:self]; } - (void)pullLoadMoreHander { if (!self.nextUrl) { TPLOG(@"数据有问题,无法获取新数据"); [self loadSuccess]; return; } [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } #pragma mark - table view delegate and datasource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.qaDataSource?self.qaDataSource.count:0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.qaHeightArray[indexPath.row] floatValue]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id data =self.qaDataSource[indexPath.row]; commentObjectVO *commentBO = data; if ([commentBO.type intValue] == 2) { return [self getAskContentCell:tableView cellForRowAtIndexPath:indexPath]; //问题(新追问) }else { return [self getAnserContentCell:tableView 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 = self.qaDataSource[indexPath.row]; cell.delegate = self; cell.indexPath = indexPath; cell.isHaveCopyMenu = NO; cell.isNeedTizhu = NO; __block NSIndexPath* IndexPath = indexPath; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { if (isExpand) { CGFloat offset = cellHeight - interaction4LinesSpace; NSString *qaAllHeightStr = Self.qaHeightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]+offset; [Self.qaHeightArray replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",qaAllHeight]]; NSArray *array = @[IndexPath]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationAutomatic]; }else { CGFloat offset = cellHeight - interaction4LinesSpace; NSString *qaAllHeightStr = Self.qaHeightArray[IndexPath.row]; CGFloat qaAllHeight = [qaAllHeightStr floatValue]-offset; [Self.qaHeightArray 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; } //问题内容 - (hotAskContentCell*)getAskContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"askContentCell"; hotAskContentCell *cell = (hotAskContentCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[hotAskContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; cell.askContentDelegate = self; cell.isHaveMenu = NO; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[askContentCell class]]) { commentObjectVO* commentData = self.qaDataSource[indexPath.row]; hotAskListHomeController *hotAskVC = [[hotAskListHomeController alloc]init]; hotAskVC.commentBO = commentData; hotAskVC.isPush = YES; hotAskVC.delegate = self; [(MLNavigationController*)self.navigationController pushViewControllerWithAnimateFlip:hotAskVC]; commentData.unNums = @"0"; [self.qaDataSource replaceObjectAtIndex:indexPath.row withObject:commentData]; [self.tableView reloadData]; } } - (void)goToOriginContent:(objInfoBO *)commentBO presentedController:(TPHttpController *)presentedController { NSDictionary *dic = @{@"data":commentBO}; [[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic]; } -(void)hotAskHomeToTopic:(TopicInfoBO *)topic user:(userBO *)user{ // NSDictionary *dic = @{@"data":topic,@"user":user}; if ([self.myAsksDelegate respondsToSelector:@selector(asksToAskCenterTopic:user:)]) { [self.myAsksDelegate asksToAskCenterTopic:topic user:user]; } // [[NSNotificationCenter defaultCenter] postNotificationName:PUSHTOTOPICDETAILCONTENT object:nil userInfo:dic]; } -(void)gotoUserInfo:(commentObjectVO *)comment{ if ([self.myAsksDelegate respondsToSelector:@selector(gotoUserGambit:)]) { [self.myAsksDelegate gotoUserGambit:comment]; } } -(void)gotoPersonInfo:(commentObjectVO *)comment{ if ([self.myAsksDelegate respondsToSelector:@selector(gotoUserGambit:)]) { [self.myAsksDelegate gotoUserGambit:comment]; } } #pragma mark - remote handler - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { NSArray *qaTempList = responsData[@"qaList"]; NSMutableArray *qaArray = [NSMutableArray array]; [qaTempList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); NSString *newNums = obj[@"newNums"]; if (newNums) { commentBO.unNums = newNums; } [qaArray addObject:commentBO]; }]; self.qaList = qaArray; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; [self endRefresh]; }else { NSArray *qaTempList = responsData[@"qaList"]; NSMutableArray *qaArray = [NSMutableArray arrayWithArray:self.qaList]; [qaTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [qaArray addObject:commentBO]; }]; self.qaList = [NSMutableArray arrayWithArray:qaArray]; [self loadSuccess]; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } } - (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