// // letterController.m // ThePaperHD // // Created by liyuan on 15/7/15. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "letterController.h" #import "letterCell.h" #import "messageViewModel.h" #import "letterInfoController.h" #import "MLNavigationController.h" @interface letterController (){ NSMutableArray *heightArray; } @property(nonatomic, strong)messageViewModel *model; @property(nonatomic, strong)NSMutableArray *dataList; @property(nonatomic, strong)UILabel *noDataView; @end @implementation letterController @synthesize dataList = _dataList; @synthesize isRequest = _isRequest; static NSString *letterCellID = @"letterCell"; - (void)viewDidLoad { [super viewDidLoad]; self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self.view addSubview:self.noDataView]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[letterCell class] forCellReuseIdentifier:letterCellID]; self.model = messageViewModel.new; self.model.delegate = self; [self configCanRefresh:YES canLoad:YES]; //推送消息:从推送消息列表进入一篇新闻后切换成夜间模式后返回,推送列表页及我的消息页显示异常(bug:5844) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; // [self needNoFirstAutoRefresh]; } - (void)needrefreshNightMode:(id)sender{ self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; self.tableView.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; [self.tableView reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)setIsRequest:(BOOL)request{ _isRequest = request; if (_isRequest) { [self firstAutoRefreshHandler]; } } -(void)pullLoadMoreHander{ if (isBlankString(self.nextUrl)) { // TPLOG(@"数据有问题,无法获取新数据"); [self loadFailed]; return; } self.model.nextUrl = self.nextUrl; } -(void)pullRefreshHandler{ self.model.letterDic = nil; } #pragma mark -- click top - (void)scrollTableViewToTop { [self.tableView setContentOffset:CGPointZero animated:YES]; } #pragma mark -- setData -(void)setDataList:(NSMutableArray *)list{ _dataList = list; if (_dataList.count<=0) { self.tableView.hidden = YES; self.noDataView.hidden = NO; }else{ self.tableView.hidden = NO; self.noDataView.hidden = YES; heightArray = [NSMutableArray array]; [_dataList enumerateObjectsUsingBlock:^(letterBO *bo, NSUInteger idx, BOOL *stop) { // CGFloat titleHeight = heightForString(bo.title, appFont(TEXT_FOUR_LEVELSIZE, NO), rect_screen.size.width-40, NSLineBreakByWordWrapping); CGFloat titleHeight = returnTextHeightWithRTLabel(bo.title, rect_screen.size.width -40, appFont(TEXT_FOUR_LEVELSIZE, NO), 7); [heightArray addObject:@(titleHeight + 88)]; }]; [self.tableView reloadData]; } } #pragma mark -- nodata view -(UILabel *)noDataView{ if (!_noDataView) { _noDataView = [[UILabel alloc]initWithFrame:CGRectZero]; _noDataView.text = @"暂无私信"; _noDataView.font = appFont(17, NO); _noDataView.textColor = [UIColor colorWithHexString:LIGHTGRAY]; _noDataView.textAlignment = NSTextAlignmentCenter; _noDataView.hidden = YES; } return _noDataView; } #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 { letterCell *cell = [tableView dequeueReusableCellWithIdentifier:letterCellID]; if (nil == cell) { cell = [[letterCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:letterCellID]; } cell.letterBo = _dataList[indexPath.row]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [MobClick event:@"84"]; [[UIApplication sharedApplication]beginIgnoringInteractionEvents]; letterBO *selectBO = _dataList[indexPath.row]; if ([selectBO.linkType intValue] == 0) {//站内信 letterInfoController *infoVC = letterInfoController.new; infoVC.letterId = selectBO.letterId; [self.navigationController pushViewController:infoVC animated:YES]; }else{ pushLetterPageWithLetterBO(self.navigationController,selectBO); } [[UIApplication sharedApplication]endIgnoringInteractionEvents]; } -(void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; self.noDataView.frame = CGRectMake(0, 140, CGRectGetWidth(self.view.bounds), 30); } #pragma mark -- model return data -(void)returnletterList:(NSMutableArray *)data nextUrl:(NSString *)url{ self.dataList = data; self.nextUrl = url; [self endRefresh]; } -(void)returnNextData:(NSMutableArray *)data nextUrl:(NSString *)url{ NSMutableArray *oldDataList = [NSMutableArray arrayWithArray:self.dataList]; [oldDataList addObjectsFromArray:data]; self.dataList = oldDataList; [self loadSuccess]; self.nextUrl = url; [self.tableView reloadData]; } -(void)returnNoData{ [self endRefresh]; } @end