// // UITestViewController.m // ThePaperDemo // // Created by Scar on 14-9-3. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "channalAnswerController.h" #import "hotAskContentCell.h" #import "answerContentCell.h" #import "hotAskFootCell.h" @interface channalAnswerController () { void (^selectItemBlock)(id listOBJ); NSInteger numsOfNewQa; void (^goToAskDetailPage)(id commentObj); } @property(nonatomic, strong)NSMutableArray *qaDataSource; //页面真实数据源(新追问) @end @implementation channalAnswerController @synthesize dataList = _dataList; @synthesize qaDataSource; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.dataKey = @"qaList"; self.nodeId = @"-2"; } - (void)setDataList:(NSMutableArray *)list { if (!_dataList || _dataList.count == 0) { self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; } _dataList = list; numsOfNewQa = 0; self.cellHeightList = [NSMutableArray array]; self.qaDataSource = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(commentObjectVO* obj, NSUInteger idx, BOOL *stop) { numsOfNewQa = numsOfNewQa + 2 + obj.answerList.count; [self.qaDataSource addObject:obj]; CGFloat titleHeight = 30+25+returnTextHeightWithRTLabel(obj.content,285, appFont(TEXT_THREE_LEVELSIZE, NO),7); //(文章标题+姓名+内容高度) [self.cellHeightList 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 = 35/2+25+15; //空行+提问者姓名 if (returnTextHeightWithRTLabel(anserBO.content, CGRectGetWidth(self.view.bounds)-10-75/2-20, appFont(13, NO),7)>190/2) { contentHeight = contentHeight + 190/2+5; }else { contentHeight = contentHeight + returnTextHeightWithRTLabel(anserBO.content, CGRectGetWidth(self.view.bounds)-10-75/2-20, appFont(13, NO),7); contentHeight = contentHeight - 15; } if (anserBO.quoteInfo && anserBO.quoteInfo.allKeys.count>0) {//假如引用信息(盖楼) contentHeight = contentHeight + 75; } [self.cellHeightList addObject:[NSString stringWithFormat:@"%f",contentHeight]]; }]; [self.qaDataSource addObject:@"0"]; //无更多回答 CGFloat footHeight = 10; [self.cellHeightList addObject:[NSString stringWithFormat:@"%f",footHeight]]; }]; [self.tableView reloadData]; } #pragma mark - tableView delegate and datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return numsOfNewQa; } #pragma mark - TPTableView delegate - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id data =self.qaDataSource[indexPath.row]; if ([data isKindOfClass:[commentObjectVO class]]) { commentObjectVO *commentBO = data; if ([commentBO.type intValue] == 2) { return [self getAskContentCell:tableView cellForRowAtIndexPath:indexPath]; //问题(新追问) }else { return [self getAnserContentCell:tableView cellForRowAtIndexPath:indexPath]; //回答(新追问) } }else { // NSString *type = data; //foot(热追问) return [self getAskFootCell:tableView cellForRowAtIndexPath:indexPath]; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[hotAskContentCell class]]) { if (goToAskDetailPage) { id data =self.qaDataSource[indexPath.row]; goToAskDetailPage(data); } } } //回答详情 - (answerContentCell*)getAnserContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"answerContentCell"; answerContentCell *cell = (answerContentCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[answerContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } cell.commentBO = self.qaDataSource[indexPath.row]; __block NSIndexPath* IndexPath = indexPath; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { if (isExpand) { cellHeight = cellHeight +35/2+25+15+5; [Self.cellHeightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; }else { cellHeight = 190/2 +35/2+25+15+5; [Self.cellHeightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; [Self.tableView beginUpdates]; [Self.tableView scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; [Self.tableView endUpdates]; } }]; return cell; } //问题内容(热追问) - (askContentCell*)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.delegate = self; return cell; } - (void)titleClick:(commentObjectVO *)contBO { if (selectItemBlock) { selectItemBlock(contBO); } } - (hotAskFootCell*)getAskFootCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellID = @"askFootCell"; hotAskFootCell *cell = (hotAskFootCell*)[tableView dequeueReusableCellWithIdentifier:cellID]; if (nil == cell) { cell = [[hotAskFootCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; } return cell; } #pragma mark - refresh and load method - (void)pullRefreshHandler { [self remoteAction]; } - (void)pullLoadMoreHander { [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } - (void)remoteAction { NSDictionary *dic = @{@"n":self.nodeId}; [Remote doJsonAction:1 requestUrl:channelNodeListURL parameter:dic delegate:self]; } #pragma mark - remote delegate - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { [self getRefreshDataFromDic:responsData sourceKey:self.dataKey contentClass:self.dataClass]; [self endRefresh]; [self saveQaListRemoteDataToCoreData:responsData]; }else { NSArray *contList = responsData[@"qaList"]; if (contList && contList.count > 0) { NSMutableArray *oriListData = [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *tempArray = [NSMutableArray array]; [contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commentObjectVO *contentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); [tempArray addObject:contentBO]; }]; NSInteger firstIndex = self.dataList.count; [oriListData addObjectsFromArray:tempArray]; self.needReload = NO; self.dataList = oriListData; NSMutableArray *indexList = [NSMutableArray array]; for (NSInteger i = firstIndex;i