热更新demo

TestViewController.m 23KB

    // // TestViewController.m // ThePaperBase // // Created by zhousan on 15/11/10. // Copyright © 2015年 scar1900. All rights reserved. // #import "TestViewController.h" #import "AskIconBaseCell.h" #import "AskContentBaseCell.h" #import "AnswerContentBaseCell.h" #import "AnswerPicBaseCell.h" #import "AnswerIconBaseCell.h" #import "AnswerQuoBaseCell.h" #import "askDetailController.h" #import "myDynamicController.h" #import "TPImageScannerController.h" #define CONTENTWIDTH rect_screen.size.width - 20 @interface TestViewController ()<UITableViewDataSource,UITableViewDelegate,AnswerContentDelegate,AskContentBaseDelegate,AskIconBaseDelegate,AnswerIconBaseDelegate,AnswerQuoDelegate,imageScanerDelegate,AnswerPicBaseDelegate> { NSMutableArray *qaList; NSMutableArray *heightList; } //@property(nonatomic, strong)UITableView *tableView; @end @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; 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 = UITableViewCellSeparatorStyleNone; [self configCanRefresh:YES canLoad:YES]; [self firstAutoRefreshHandler]; } #pragma mark - TPTableView delegate - (void)pullRefreshHandler { NSDictionary *dic = @{@"c":@"1263700"}; [Remote doJsonAction:1 requestUrl:contentAskURL parameter:dic delegate:self]; } - (void)pullLoadMoreHander { [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { //type Int 1 类型:1-跟帖,2-提问,3-回答 24-提问头像 34-回答头像 5-图片 if (actionTag == 1) { qaList = [NSMutableArray array]; heightList = [NSMutableArray array]; NSMutableArray *temp = responsData[@"qaList"]; [temp enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { commentObjectVO *comment = setJsonDicToDataModel(obj, [commentObjectVO class]); comment.type = @"24"; [heightList addObject:@"50"]; [qaList addObject:comment];//@"[蜡烛]",@"[赞]",@"[微笑]",@"[哈哈]",@"[挖鼻]",@"[怒]",@"[围观]",@"[悲伤]",@"[心]",@"[疑问]",@"[衰]" commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); commentBO.content = [NSString stringWithFormat:@"噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]%@",commentBO.content]; commentBO.type = @"2"; CGFloat askHeight = 15; int LabelHeigth = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:commentBO.content LineSpace:10 ParagraphSpacing:0]; commentBO.labelHeight = LabelHeigth; [qaList addObject:commentBO]; askHeight += LabelHeigth; [heightList addObject:[NSString stringWithFormat:@"%f",askHeight]]; if (comment.answerList.count >0) { commentObjectVO *anwerBO = setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class]); anwerBO.type = @"34"; [heightList addObject:@"50"]; [qaList addObject:anwerBO]; commentObjectVO *answer = setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class]); answer.type = @"3"; answer.content = [NSString stringWithFormat:@"噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]%@噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]",answer.content]; CGFloat anserHeight = 10; int anserH = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:answer.content LineSpace:10 ParagraphSpacing:0]; answer.labelHeight = anserH; [qaList addObject:answer]; CGFloat four = (appFont(TEXT_FOUR_LEVELSIZE, NO).lineHeight+10)*4; if (anserH > four) { anserH = four; anserHeight += 25; } anserHeight += anserH; [heightList addObject:[NSString stringWithFormat:@"%f",anserHeight]]; [self rebuildCommentBOInAnswer:setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class])]; } }]; [self.tableView reloadData]; [self endRefresh]; [self.tableView setContentOffset:CGPointMake(0, 1)]; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; }else { NSArray *qaTempList = responsData[@"qaList"]; [qaTempList enumerateObjectsUsingBlock:^(NSDictionary* obj, NSUInteger idx, BOOL *stop) { commentObjectVO *comment = setJsonDicToDataModel(obj, [commentObjectVO class]); comment.type = @"24"; [heightList addObject:@"50"]; [qaList addObject:comment];//@"[蜡烛]",@"[赞]",@"[微笑]",@"[哈哈]",@"[挖鼻]",@"[怒]",@"[围观]",@"[悲伤]",@"[心]",@"[疑问]",@"[衰]" commentObjectVO *commentBO = setJsonDicToDataModel(obj, [commentObjectVO class]); commentBO.content = [NSString stringWithFormat:@"噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]%@",commentBO.content]; commentBO.type = @"2"; CGFloat askHeight = 15; int LabelHeigth = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:commentBO.content LineSpace:10 ParagraphSpacing:0]; commentBO.labelHeight = LabelHeigth; [qaList addObject:commentBO]; askHeight += LabelHeigth; [heightList addObject:[NSString stringWithFormat:@"%f",askHeight]]; if (comment.answerList.count >0) { commentObjectVO *anwerBO = setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class]); anwerBO.type = @"34"; [heightList addObject:@"50"]; [qaList addObject:anwerBO]; commentObjectVO *answer = setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class]); answer.type = @"3"; answer.content = [NSString stringWithFormat:@"噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]%@噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]噶[心]个啊[疑问]gea嘎嘎嘎[疑问]伙食费阿哥[疑问][蜡烛]蜡[衰][衰][衰]烛[微笑]",answer.content]; CGFloat anserHeight = 10; int anserH = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 Text:answer.content LineSpace:10 ParagraphSpacing:0]; answer.labelHeight = anserH; [qaList addObject:answer]; CGFloat four = (appFont(TEXT_FOUR_LEVELSIZE, NO).lineHeight+10)*4; if (anserH > four) { anserH = four; anserHeight += 25; } anserHeight += anserH; [heightList addObject:[NSString stringWithFormat:@"%f",anserHeight]]; [self rebuildCommentBOInAnswer:setJsonDicToDataModel(comment.answerList[0], [commentObjectVO class])]; } }]; [self loadSuccess]; [self.tableView reloadData]; NSString *url = responsData[@"nextUrl"]; self.nextUrl = url; } } - (void)rebuildCommentBOInAnswer:(commentObjectVO *)comment { imageObjectBO *image1 = [imageObjectBO new]; image1.url = @"http://img3.imgtn.bdimg.com/it/u=387878309,984304042&fm=21&gp=0.jpg"; image1.width = @"2560"; image1.height = @"1600"; image1.tags = @"1"; imageObjectBO *image2 = [imageObjectBO new]; image2.url = @"http://img1d.xgo-img.com.cn/pics/1503/240/180/1502574.jpg"; image2.width = @"240"; image2.height = @"180"; image2.tags = @"2"; imageObjectBO *image3 = [imageObjectBO new]; image3.url = @"http://ww2.sinaimg.cn/thumb180/68065fcatw1e6ec5iflt3j206x0iodh4.jpg"; image3.width = @"180"; image3.height = @"180"; image3.tags = @"2"; switch (rand()%3) { case 0: comment.imageList = @[image1]; break; case 1: comment.imageList = @[image1,image2]; break; case 2: comment.imageList = @[image1,image2,image3]; break; default: break; } comment.type = @"5"; if (comment.imageList.count == 1) { imageObjectBO *imageB = comment.imageList[0]; CGSize imageSize = CGSizeMake([imageB.width floatValue], [imageB.height floatValue]); CGSize size = CGSizeZero; if (imageSize.width > CONTENTWIDTH) { size = CGSizeMake(CONTENTWIDTH, (imageSize.height*CONTENTWIDTH)/imageSize.width); }else { size = CGSizeMake([imageB.width floatValue], [imageB.height floatValue]); } [heightList addObject:[NSString stringWithFormat:@"%f",size.height+10]]; }else { [heightList addObject:[NSString stringWithFormat:@"%f",quo_pic_width+10]]; } [qaList addObject:comment]; if (comment.quoteInfo && comment.quoteInfo.allKeys.count > 0) { commentObjectVO *quoInfo = setJsonDicToDataModel(comment.quoteInfo, [commentObjectVO class]); quoInfo.content = @"个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个啊个[疑问]"; quoInfo.type = @"1"; switch (rand()%3) { case 0: quoInfo.imageList = @[image1]; break; case 1: quoInfo.imageList = @[image1,image2]; break; case 2: quoInfo.imageList = @[image1,image2,image3]; break; default: break; } CGFloat quoHeight = 50/2+10+15+15; int quoLabelHeight = (int)[TPEmojiLabel heightForLabelWithFont:appFont(TEXT_FOUR_LEVELSIZE, NO) Width:rect_screen.size.width - 20 - 16 Text:quoInfo.content LineSpace:10 ParagraphSpacing:0]; CGFloat twoLine = getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE); quoInfo.labelHeight = quoLabelHeight; if (quoLabelHeight > twoLine) { quoLabelHeight = twoLine; quoHeight += 15; } quoHeight += quoLabelHeight; [qaList addObject:quoInfo]; if (quoInfo.imageList.count > 0) { quoHeight += quo_pic_width+15/2; } [heightList addObject:[NSString stringWithFormat:@"%f",quoHeight]]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return qaList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { id obj = qaList[indexPath.row]; if ([obj isKindOfClass:[commentObjectVO class]]) { commentObjectVO *comment = obj; // NSLog(@"%@",comment.type); switch ([comment.type integerValue]) { case 1://跟帖 return [self returnAnswerQuoTable:tableView cellForRowAtIndexPath:indexPath]; break; case 2://提问 return [self returnAskContentTable:tableView cellForRowAtIndexPath:indexPath]; break; case 3://回答 return [self returnAnswerContentTableTable:tableView cellForRowAtIndexPath:indexPath]; break; case 24://提问头像 return [self returnAskIconTable:tableView cellForRowAtIndexPath:indexPath]; break; case 34://回答头像 return [self returnAnswerIconTable:tableView cellForRowAtIndexPath:indexPath]; break; case 5://图片 return [self returnAnswerPicTable:tableView cellForRowAtIndexPath:indexPath]; break; default: break; } } return nil; } - (AskContentBaseCell *)returnAskContentTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AskContentBaseCellId"; AskContentBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AskContentBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.delegate = self; return cell; } - (AskIconBaseCell *)returnAskIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AskIconBaseCellId"; AskIconBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AskIconBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.delegate = self; return cell; } - (AnswerIconBaseCell *)returnAnswerIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerIconBaseCellId"; AnswerIconBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AnswerIconBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.delegate = self; return cell; } - (AnswerContentBaseCell *)returnAnswerContentTableTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerContentBaseCellId"; AnswerContentBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AnswerContentBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.indexPath = index; cell.delegate = self; __block NSIndexPath *IndexPath = index; __weak typeof(self) Self = self; [cell expandCell:^(BOOL isExpand, CGFloat cellHeight) { if (isExpand) { cellHeight = cellHeight+25 + 10; [heightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; }else { CGFloat four = (appFont(TEXT_FOUR_LEVELSIZE, NO).lineHeight+10)*4; cellHeight = four+25+10; [heightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; } }]; return cell; } - (AnswerQuoBaseCell *)returnAnswerQuoTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerQuoBaseCellId"; AnswerQuoBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AnswerQuoBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.delegate = self; __block NSIndexPath *IndexPath = index; __weak typeof(self) Self = self; [cell quoBlock:^(BOOL isQuoExpand, CGFloat quoHeight) { if (isQuoExpand) { quoHeight = quoHeight+50/2+10+15+15; if (cell.commentBO.imageList.count > 0) { quoHeight += quo_pic_width+15/2; } [heightList 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 = getLineHeightWithCountAndFontWidth(2, 10, TEXT_FOUR_LEVELSIZE); quoHeight = two+50/2+10+15+15+15; if (cell.commentBO.imageList.count > 0) { quoHeight += quo_pic_width+15/2; } [heightList replaceObjectAtIndex:IndexPath.row withObject:[NSString stringWithFormat:@"%f",quoHeight]]; NSArray *array = @[IndexPath]; [Self.tableView beginUpdates]; [Self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [Self.tableView endUpdates]; } }]; return cell; } - (AnswerPicBaseCell *)returnAnswerPicTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerPicBaseCellId"; AnswerPicBaseCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[AnswerPicBaseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = qaList[index.row]; cell.commentBO = comment; cell.delegate = self; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [heightList[indexPath.row] floatValue]; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { UIMenuController *menu = [UIMenuController sharedMenuController]; if (menu.menuVisible) { [menu setMenuVisible:NO animated:YES]; return; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - cell delegate - (void)gotoHotAskWithCommentVO:(commentObjectVO *)comment { [self gotoHotAsk:comment]; } - (void)gotoHotAsk:(commentObjectVO *)comment { askDetailController *hotAskVC = [[askDetailController alloc]init]; hotAskVC.commentBO = comment; [self.navigationController pushViewController:hotAskVC animated:YES]; } - (void)askIconBaseGotoHotAsk:(commentObjectVO *)comment { [self gotoHotAsk:comment]; } - (void)askIconBaseGotoUserInfo:(commentObjectVO *)comment { [self gotoUser:comment]; } - (void)gotoUserInfo:(commentObjectVO *)comment { [self gotoUser:comment]; } - (void)gotoUser:(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)praiseHandlerWithIndexPath:(NSIndexPath *)indexPath { NSIndexPath *index = [NSIndexPath indexPathForRow:indexPath.row-1 inSection:indexPath.section]; AnswerIconBaseCell *cell = (AnswerIconBaseCell *)[self.tableView cellForRowAtIndexPath:index]; [cell commentOK:cell.OkButton]; } - (void)tapPicWithIndex:(NSUInteger)tag andImageBOArray:(NSArray *)imageArray { [self gotoImageScannerWithIndex:tag-1 imageArray:[NSMutableArray arrayWithArray:imageArray]]; } - (void)clickImageWithIndex:(NSUInteger)index ImageArray:(NSArray *)imageArray { [self gotoImageScannerWithIndex:index-1 imageArray:[NSMutableArray arrayWithArray:imageArray]]; } - (void)gotoImageScannerWithIndex:(NSUInteger)index imageArray:(NSMutableArray *)imageArray { ((MLNavigationController *)self.navigationController).canDragBack = NO; TPImageScannerController *imageScanner = [[TPImageScannerController alloc] init]; [self.navigationController pushViewController:imageScanner animated:NO]; [UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) { imageScanner.currentIndex = index; imageScanner.imageBOList = imageArray; imageScanner.type = contentImageScannerType; imageScanner.delegate = self; }]; } - (void)imageContentClick:(imageObjectBO*)imageBO { ((MLNavigationController *)self.navigationController).canDragBack = YES; [self.navigationController popViewControllerAnimated:NO]; [UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil]; } /* #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