热更新demo

letterInfoTableController.m 11KB

    // // letterInfoTableController.m // ThePaperBase // // Created by YoungLee on 15/9/15. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "letterInfoTableController.h" #import "AsyncImageView.h" #import "messageViewModel.h" #import "letterInfoTopicCell.h" #import "letterInfoImageCell.h" #import "letterInfoContentCell.h" #import "Reachability.h" @interface letterInfoTableController ()<messageViewModelDelegate,UITableViewDataSource,UITableViewDelegate>{ NSMutableArray *heightArray; CGFloat changeHieght; } @property(nonatomic, strong)messageViewModel *model; @property(nonatomic, strong)NSString *letterUrl; @property(nonatomic, strong)NSMutableArray *dataList; @end @implementation letterInfoTableController @synthesize letterId = _letterId; @synthesize dataList = _dataList; @synthesize letterUrl = _letterUrl; static NSString *infoTopicCellID = @"letterInfoTopicCell"; static NSString *infoImageCellID = @"letterInfoImageCell"; static NSString *infoContentCellID = @"letterInfoContentCell"; - (void)viewDidLoad { [super viewDidLoad]; self.tableView.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.clipsToBounds = YES; [self configCanRefresh:NO canLoad:YES]; self.tableView.tableFooterView = [UIView new]; [self needNoFirstAutoRefresh]; self.model = messageViewModel.new; self.model.delegate = self; self.model.letterDic = @{@"letterId":_letterId}; // self.tableView.tableFooterView = nil; [self.tableView registerClass:[letterInfoTopicCell class] forCellReuseIdentifier:infoTopicCellID]; [self.tableView registerClass:[letterInfoImageCell class] forCellReuseIdentifier:infoImageCellID]; [self.tableView registerClass:[letterInfoContentCell class] forCellReuseIdentifier:infoContentCellID]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; // Do any additional setup after loading the view. } #pragma mark -- set Data -(void)setLetterId:(NSString *)leId{ _letterId = leId; } -(void)setLetterUrl:(NSString *)url{ _letterUrl = url; if (!isBlankString(url)) { self.nextUrl = url; } } -(void)pullLoadMoreHander{ if(isBlankString(self.letterUrl)){// 私信详情页,拉到底部要有提示“已到底了!”(bug:5080) ShowTextMessage(@"已到底了"); [self loadFailed]; return; } } -(void)setDataList:(NSMutableArray *)list{ _dataList = list; heightArray = [NSMutableArray array]; [_dataList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { if ([obj isKindOfClass:[letterBO class]]) { letterBO *letterBo = obj; // UILabel *_topicLabel = [[UILabel alloc]initWithFrame:CGRectZero]; // _topicLabel.font = appFont(TEXT_TWO_LEVELSIZE, NO); // _topicLabel.textColor = [UIColor colorWithHexString:TextBlack]; // _topicLabel.lineBreakMode = NSLineBreakByWordWrapping; // _topicLabel.numberOfLines = 0; // _topicLabel.backgroundColor = [UIColor clearColor]; // _topicLabel.text = letterBo.title; // CGFloat titleHeight = [_topicLabel sizeThatFits:CGSizeMake(rect_screen.size.width -20, NO)].height; CGFloat titleHeight = returnTextHeightWithRTLabel(letterBo.title, rect_screen.size.width -20, appFont(TEXT_THREE_LEVELSIZE, NO), 7); [heightArray addObject:@(titleHeight + 48)]; }else if ([obj isKindOfClass:[imageObjectBO class]]){ imageObjectBO *imageBO = obj; if (isBlankString(imageBO.url)) { [heightArray addObject:@(0)]; }else{ CGFloat imageWidth; if ([imageBO.width floatValue] >= rect_screen.size.width -30) { imageWidth = rect_screen.size.width-30; }else{ imageWidth = [imageBO.width floatValue]; } CGFloat imageHeight = [imageBO.height floatValue]; CGFloat scale = imageHeight/imageWidth; changeHieght = 200*scale +20; if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [heightArray addObject:@(200*scale + 20)]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { [heightArray addObject:@(0)]; }else { if ([Remote IsEnableWIFI]) { [heightArray addObject:@(200*scale + 20)]; }else { [heightArray addObject:@(0)]; } } } }else if ([obj isKindOfClass:[NSString class]]){ NSString *contentStr = obj; // CGFloat contentHeight = returnTextHeightWithRTLabel(contentStr, rect_screen.size.width -20, appFont(TEXT_FOUR_LEVELSIZE, NO), 7); CGFloat contentHeight = heightForAttributeString(getLineSpaceAttributedString(contentStr, 7, appFont(TEXT_FOUR_LEVELSIZE, NO)), rect_screen.size.width - 20, appFont(TEXT_FOUR_LEVELSIZE, NO)); [heightArray addObject:@(contentHeight+10)]; } }]; [self.tableView reloadData]; } #pragma mark -- tableView - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _dataList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [heightArray[indexPath.row] floatValue]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([_dataList[indexPath.row] isKindOfClass:[letterBO class]]) { return [self getInfoTopicCell:tableView cellForRowAtIndexPath:indexPath]; }else if ([_dataList[indexPath.row] isKindOfClass:[imageObjectBO class]]){ return [self getInfoImageCell:tableView cellForRowAtIndexPath:indexPath]; }else{ return [self getInfoContentCell:tableView cellForRowAtIndexPath:indexPath]; } } - (UITableViewCell*)getInfoTopicCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { letterInfoTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:infoTopicCellID]; if (nil == cell) { cell = [[letterInfoTopicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:infoTopicCellID]; } cell.letterBo = _dataList[indexPath.row]; return cell; } - (UITableViewCell*)getInfoImageCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { letterInfoImageCell *cell = [tableView dequeueReusableCellWithIdentifier:infoImageCellID]; if (nil == cell) { cell = [[letterInfoImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:infoImageCellID]; } cell.imageBo = _dataList[indexPath.row]; return cell; } - (UITableViewCell*)getInfoContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { letterInfoContentCell *cell = [tableView dequeueReusableCellWithIdentifier:infoContentCellID]; if (nil == cell) { cell = [[letterInfoContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:infoContentCellID]; } cell.contentStr = _dataList[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [[UIApplication sharedApplication]beginIgnoringInteractionEvents]; UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell isKindOfClass:[letterInfoImageCell class]]) { if ([self.delegate respondsToSelector:@selector(gotoImage:)]) { imageObjectBO *imageBO = self.dataList[indexPath.row]; imageBO.tags = @"WWW"; [self.delegate gotoImage:imageBO]; } } [[UIApplication sharedApplication]endIgnoringInteractionEvents]; } - (void) reachabilityChanged:(NSNotification *)note { NSIndexPath *changeIndex = [NSIndexPath indexPathForRow:1 inSection:0]; if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [heightArray replaceObjectAtIndex:1 withObject:@(changeHieght)]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:changeIndex] withRowAnimation:UITableViewRowAnimationFade]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { [heightArray replaceObjectAtIndex:1 withObject:@"0"]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:changeIndex] withRowAnimation:UITableViewRowAnimationFade]; }else { if ([Remote IsEnableWIFI]) { [heightArray replaceObjectAtIndex:1 withObject:@(changeHieght)]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:changeIndex] withRowAnimation:UITableViewRowAnimationFade]; }else { [heightArray replaceObjectAtIndex:1 withObject:@"0"]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:changeIndex] withRowAnimation:UITableViewRowAnimationFade]; } } } #pragma mark - btn event //- (void)closeEvent:(UIButton*)btn { // [(MLNavigationController*)self.navigationController popViewControllerWithFlip]; //} #pragma mark -- viewModel return data -(void)returnletterInfo:(letterBO *)data{ self.letterUrl = @""; imageObjectBO *imageBo = setJsonDicToDataModel(data.image, [imageObjectBO class]); if (imageBo.url && !isBlankString(imageBo.url)) { NSMutableArray *array = [NSMutableArray arrayWithObjects:data,imageBo,data.detail, nil]; self.dataList = array; }else { NSMutableArray *array = [NSMutableArray arrayWithObjects:data,data.detail, nil]; self.dataList = array; } } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (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