热更新demo

HotCommentTableViewVC.m 12KB

    // // HotCommentTableViewVC.m // ThePaperBase // // Created by zhousan on 15/8/26. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "HotCommentTableViewVC.h" #import "HotCommentCell.h" #import "NewsForHotCommentCell.h" #import "detailContentHomeController.h" #import "askDetailController.h" #import "myDynamicController.h" #import "HotCommentAnswerIconCell.h" #import "HotCommentContentcell.h" #import "ImageShareController.h" @interface HotCommentTableViewVC () <AnswerContentDelegate,AnswerIconBaseDelegate> @property(nonatomic, strong) NSMutableArray *commentList; @property(nonatomic, strong) NSMutableArray *dataList; @property(nonatomic, strong) NSMutableArray *heightList; @end @implementation HotCommentTableViewVC - (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 *dic = @{@"comment":obj}; CGFloat newsHeight = returnTextHeightWithRTLabel([NSString stringWithFormat:@"<font color='#8e8f90'>【原新闻】</font><N>%@",obj.contName], rect_screen.size.width-20-20, appFont(15, NO), 7)+30; [self.heightList addObject:[NSString stringWithFormat:@"%f",newsHeight]]; // obj.content = [obj.content stringByReplacingOccurrencesOfString:@"\r" withString:@"0"]; [self.dataList addObject:dic]; NSDictionary *answerdic = setDataModelToDic(obj, [commentObjectVO class]); commentObjectVO *anwerBO = setJsonDicToDataModel(answerdic, [commentObjectVO class]); anwerBO.type = @"34"; [self.heightList addObject:@"50"]; [self.dataList addObject:anwerBO]; commentObjectVO *answer = setJsonDicToDataModel(answerdic, [commentObjectVO class]); answer.type = @"3"; 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]; anserHeight += anserH; [self.heightList addObject:[NSString stringWithFormat:@"%f",anserHeight]]; }]; [self.tableView reloadData]; } #pragma mark - tableDataSouce - (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]]) { switch ([[(commentObjectVO *)obj type] integerValue]) { case 34: return [self returnAnswerIconTable:table cellForRowAtIndexPath:indexPath]; break; case 3: return [self returnAnswerContentTableTable:table cellForRowAtIndexPath:indexPath]; break; default: return nil; break; } }else if ([obj isKindOfClass:[NSDictionary class]]) { return [self returnNewsForHotCommentCellWithTableView:table cellForRowAtIndexPath:indexPath]; } return nil; } - (HotCommentAnswerIconCell *)returnAnswerIconTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerIconBaseCellId"; HotCommentAnswerIconCell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[HotCommentAnswerIconCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = self.dataList[index.row]; cell.commentBO = comment; cell.delegate = self; return cell; } - (HotCommentContentcell *)returnAnswerContentTableTable:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)index { static NSString *cellId = @"AnswerContentBaseCellId"; HotCommentContentcell *cell = [table dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[HotCommentContentcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; } commentObjectVO *comment = self.dataList[index.row]; cell.commentBO = comment; cell.indexPath = index; cell.delegate = self; return cell; } - (NewsForHotCommentCell *)returnNewsForHotCommentCellWithTableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *NewsForHotCommentCellId = @"NewsForHotCommentCellId"; NewsForHotCommentCell *cell = [table dequeueReusableCellWithIdentifier:NewsForHotCommentCellId]; if (nil == cell) { cell = [[NewsForHotCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsForHotCommentCellId]; } 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"]; detailContentHomeController *vc = detailContentHomeController.new; objInfoBO *objInfo = setJsonDicToDataModel(comment.objInfo, [objInfoBO class]); vc.nodeID = objInfo.forwordNodeId; [self.navigationController pushViewController:vc animated:YES]; addHaveReadFlag(objInfo.forwordNodeId); } } #pragma mark - TPTableView delegate - (void)pullRefreshHandler { [Remote doJsonAction:1 requestUrl:HotCommentURL parameter:nil delegate:self]; } - (void)pullLoadMoreHander { [Remote doJsonAction:2 requestUrl:self.nextUrl parameter:nil delegate:self]; } - (void)gotoCommentWith:(commentObjectVO *)comment { if ([TPUserDefault instance].userBO) { if ([self.delegate respondsToSelector:@selector(presentWriteToUser:)]) { [self.delegate presentWriteToUser:comment]; } }else { if ([self.delegate respondsToSelector:@selector(commentPushToLoginView)]) { [self.delegate commentPushToLoginView]; } } // askDetailController *hotAskVC = [[askDetailController alloc]init]; // hotAskVC.commentBO = comment; // [self.navigationController pushViewController:hotAskVC animated:YES]; // [hotAskVC gotoCommentFromHotAnwser:comment]; // } #pragma mark - remote delegate - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { NSMutableArray* commentList = [NSMutableArray array]; NSArray *list = responsData[@"commentList"]; 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)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)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]; CGFloat topicHeight = heightForAttributeStringWithLabel(getLineSpaceAttributedString(comment.contName, 3, appFont(TEXT_SIX_LEVELSIZE, NO)), 160, appFont(TEXT_SEVEN_LEVELSIZE, NO)); CGFloat askHeight = 5 + heightForAttributeStringWithLabel(getLineSpaceAttributedString(comment.content, 6, appFont(TEXT_FIVE_LEVELSIZE, NO)), 170, appFont(TEXT_FIVE_LEVELSIZE, NO)); imageShareController.frame = CGRectMake(0, 0, 240, 280+topicHeight+askHeight); imageShareController.askData = comment; UIImage* img = [self capture:imageShareController]; if (!img) { ShowTextMessage(@"图片未完成"); return; } // UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); contentObjectBO *askContentBO = [contentObjectBO new]; askContentBO.name = comment.contName; askContentBO.summary = comment.content; askContentBO.shareUrl = comment.objInfo[@"shareUrl"]; if ([self.delegate respondsToSelector:@selector(hotCommentTableToShare:contentBO:)]) { [self.delegate hotCommentTableToShare: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