热更新demo

channalTopicController.m 35KB

    // // channalTopicController.m // ThePaperBase // // Created by scar1900 on 15/8/3. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "channalTopicController.h" #import "channelTopicSearchCell.h" #import "channelTopicContentCell.h" #import "channelTopicCateCell.h" #import "InterViewPageViewController.h" #import "topicSeachController.h" #import "topicContentController.h" #import "channelListBannerCell.h" #import "Reachability.h" #import "topicCollectionCoverFlowCell.h" #import "topicCollectionController.h" #import "creatTopicController.h" #import "loginHomeController.h" #define ALLDATATAG 1001 #define NEXTURLTAG 1002 #define CATETAG 1003 #define channelListAdScale (float)338/600 @interface channalTopicController()<channelTopicCateDelegate,bannerCellDelgate,topicCollectionCoverFlowDelegate>{ BOOL needManualRefresh; //需要接受通知刷新标志位 NSMutableArray *heightList; NSMutableArray *dataSourceList; NSMutableArray *cateList; BOOL searchExist; CGFloat newContentOffsetY; CGFloat lastContentOffsetY; NSInteger directionFlag; //1:向上滚 2:向下滚 NSInteger refreshFlag; //0:不刷新 1:刷新数据 } @property(nonatomic, strong)NSString *topicNextUrl; @property(nonatomic, strong) NSMutableArray *dataList; @property(nonatomic ,strong)UIView *foorView; @property(nonatomic, strong)NSMutableArray *topicCollectionList; @property(nonatomic, strong)UIButton *createTopicBtn; @property(nonatomic, strong)UIView *searchView; @end @implementation channalTopicController @synthesize dataList = _dataList; @synthesize currentIndex = _currentIndex; @synthesize nodeId = _nodeId; @synthesize topicNextUrl = _topicNextUrl; @synthesize searchView; //@synthesize nextUrl = _nextUrl; static NSString *topicSearchCellID = @"channelTopicSearchCell"; static NSString *topicContentCellID = @"channelTopicContentCell"; static NSString *topicCateCellID = @"channelTopicCateCell"; static NSString *topicCollectionCoverFlowCellID = @"topicCollectionCoverFlowCell"; - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // if (searchExist && 0 >= self.tableView.contentOffset.y) { // //NSLog(self.tableView.tableHeaderView.hidden ? @"Yes" : @"No"); // searchView.hidden = NO; // self.tableView.tableHeaderView = searchView; // //NSLog(self.tableView.tableHeaderView.hidden ? @"Yes" : @"No"); // } if (self.refreshHeader.state != RefreshStateNormal) { lastContentOffsetY = 0; return; } if (![self.tableView.tableFooterView isKindOfClass:[channelTopicCateCell class]] && self.loadFooter.state != RefreshStateNormal) { return; } //bug5470: 【倒退】话题精选:下拉刷新,刷新效果不显示了 if (self.dataList.count == 0) { return; } if(!searchExist && 0 > self.tableView.contentOffset.y){ self.tableView.contentInset = UIEdgeInsetsMake(-50 - self.tableView.contentOffset.y, 0,0, 0); searchView = [self getSearchCell:self.tableView]; searchView.frame = CGRectMake(0, 0, rect_screen.size.width, 50); self.tableView.tableHeaderView = searchView; UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom]; searchBtn.frame = searchView.bounds; [searchBtn addTarget:self action:@selector(gotoSearchSelector:) forControlEvents:UIControlEventTouchUpInside]; [searchView addSubview:searchBtn]; lastContentOffsetY += 50; searchExist = YES; } //NSLog(@"%f", self.createTopicBtn.frame.origin.y); scrollView.contentInset = UIEdgeInsetsZero; scrollView.scrollIndicatorInsets = UIEdgeInsetsZero; newContentOffsetY = scrollView.contentOffset.y; //根据触摸相对位置做判断 if (newContentOffsetY > lastContentOffsetY && scrollView.dragging) { // 向上滚动 if (newContentOffsetY >0) { directionFlag = 1; CGRect tabFrame = self.createTopicBtn.frame; if (tabFrame.origin.y < CGRectGetHeight(self.view.bounds)) { CGFloat length = newContentOffsetY - lastContentOffsetY; tabFrame.origin.y += length; if (tabFrame.origin.y > CGRectGetHeight(self.view.bounds)) { tabFrame.origin.y = CGRectGetHeight(self.view.bounds); } self.createTopicBtn.frame = tabFrame; CGFloat temp = CGRectGetHeight(self.view.bounds)-tabFrame.origin.y; if (temp >= 10) temp -= 10; else temp = 0; [self.createTopicBtn setAlpha:temp/50]; } } }else if (newContentOffsetY < lastContentOffsetY && scrollView.dragging) { // 向下滚动 CGRect frame = scrollView.frame; if (newContentOffsetY >= scrollView.contentSize.height - frame.size.height || scrollView.contentSize.height < frame.size.height) { }else { directionFlag = 2; CGRect tabFrame = self.createTopicBtn.frame; if (tabFrame.origin.y > CGRectGetHeight(self.view.bounds)-60) { CGFloat length = lastContentOffsetY - newContentOffsetY; tabFrame.origin.y -= length; if (tabFrame.origin.y < CGRectGetHeight(self.view.bounds)-60) { tabFrame.origin.y = CGRectGetHeight(self.view.bounds)-60; } self.createTopicBtn.frame = tabFrame; CGFloat temp = CGRectGetHeight(self.view.bounds)-tabFrame.origin.y; if (temp >= 10) temp -= 10; else temp = 0; [self.createTopicBtn setAlpha:temp/50]; } } } lastContentOffsetY = scrollView.contentOffset.y; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { if (self.refreshHeader.state != RefreshStateNormal) { return; } if (![self.tableView.tableFooterView isKindOfClass:[channelTopicCateCell class]] && self.loadFooter.state != RefreshStateNormal) { return; } //bug5470: 【倒退】话题精选:下拉刷新,刷新效果不显示了 if (directionFlag == 1) { [UIView animateWithDuration:0.3 animations:^{ CGRect tabFrame = self.createTopicBtn.frame; if (tabFrame.origin.y != CGRectGetHeight(self.view.bounds)) { tabFrame.origin.y = CGRectGetHeight(self.view.bounds); } self.createTopicBtn.frame = tabFrame; [self.createTopicBtn setAlpha:0.0]; } completion:^(BOOL finished) { directionFlag = 0; }]; } else if (directionFlag == 2) { [UIView animateWithDuration:0.3 animations:^{ CGRect tabFrame = self.createTopicBtn.frame; if (tabFrame.origin.y != CGRectGetHeight(self.view.bounds)-60) { tabFrame.origin.y = CGRectGetHeight(self.view.bounds)-60; } self.createTopicBtn.frame = tabFrame; [self.createTopicBtn setAlpha:1.0]; } completion:^(BOOL finished) { directionFlag = 0; }]; } } //- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { // if ((scrollView.contentSize.height-scrollView.frame.size.height-scrollView.contentOffset.y) > -1 && // (scrollView.contentSize.height-scrollView.frame.size.height-scrollView.contentOffset.y) < 1) { // [UIView animateWithDuration:0.1 animations:^{ // CGRect tabFrame = self.createTopicBtn.frame; // if (tabFrame.origin.y != CGRectGetHeight(self.view.bounds)) { // tabFrame.origin.y = CGRectGetHeight(self.view.bounds); // } // self.createTopicBtn.frame = tabFrame; // [self.createTopicBtn setAlpha:0.0]; // } completion:^(BOOL finished) { // directionFlag = 0; // }]; // } //} - (void)pageChangeBegin:(NSInteger)index { // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.currentIndex = [NSString stringWithFormat:@"%ld",(long)index]; if (self.dataList.count == 0) { NSDictionary *channelDic = [TPUserDefault instance].channldataList[[self.currentIndex intValue]]; self.nodeId = channelDic[@"nodeId"]; } [self firstAutoRefreshHandler]; if (needManualRefresh) {//切换到页面检查是否需要刷新 [self manualRefresh]; needManualRefresh = NO; } // }); } - (void)viewDidLoad { [super viewDidLoad]; self.tableView.clipsToBounds = YES; self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; needManualRefresh = NO; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; // [self.tableView registerClass:[channelTopicSearchCell class] forCellReuseIdentifier:topicSearchCellID]; [self.tableView registerClass:[channelTopicContentCell class] forCellReuseIdentifier:topicContentCellID]; [self.tableView registerClass:[channelTopicCateCell class] forCellReuseIdentifier:topicCateCellID]; [self.tableView registerClass:[topicCollectionCoverFlowCell class] forCellReuseIdentifier:topicCollectionCoverFlowCellID]; [self.tableView registerClass:[channelTopicSearchCell class] forHeaderFooterViewReuseIdentifier:topicSearchCellID]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFrontPage) name:NEEDREFRESHENTERFORGROUND object:nil]; //问吧:切换无图模式,不能立即生效(bug:5041) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needrefreshNightMode:) name:REFRESHAFTERNIGHTMODE object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(needRefreshReadMode:) name:NEEDREFRESHCHANGEREADMODE object:nil]; [self remoteCate]; [self configCanRefresh:YES canLoad:YES]; [self.view addSubview:self.createTopicBtn]; directionFlag = 0; searchExist = NO; refreshFlag = 0; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.createTopicBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-60, CGRectGetHeight(self.view.bounds)-60, 60, 60); [self.createTopicBtn setAlpha:1.0]; } - (UIButton*)createTopicBtn { if (!_createTopicBtn) { _createTopicBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _createTopicBtn.backgroundColor = [UIColor clearColor]; [_createTopicBtn setImage:Image(@"Button/createTopic.png") forState:UIControlStateNormal]; [_createTopicBtn setImage:Image(@"Button/createTopic_s.png") forState:UIControlStateHighlighted]; [_createTopicBtn addTarget:self action:@selector(createTopicBtnSelector) forControlEvents:UIControlEventTouchUpInside]; } return _createTopicBtn; } - (void)needrefreshNightMode:(id)sender{ self.view.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; } //进入后台后回到前台的间隔时间大于一小时 - (void)refreshFrontPage { needManualRefresh = YES; if (self.currentIndex && [self.currentIndex intValue] == [TPUserDefault instance].currentPageIndex) { needManualRefresh = NO; [self manualRefresh]; } } - (void)startLoad { [self pullLoadMoreHander]; } -(void)pullRefreshHandler{ // [self configCanRefresh:YES canLoad:YES]; if(self.foorView){ self.tableView.tableFooterView = self.foorView; } [self remoteAction]; } -(void)pullLoadMoreHander{ if (isBlankString(self.topicNextUrl)) { if (!self.foorView && [self.tableView.tableFooterView isKindOfClass:[loadFootView class]]) { self.foorView = self.tableView.tableFooterView; } //bug5480: 【倒退】话题精选页:拉到底部话题分类不显示了 //TPLOG(@"数据有问题,无法获取新数据 "); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ ShowTextMessage(@"点击分类,查看更多精彩话题"); }); [self loadFailed]; long columNum; if(cateList.count % 4 != 0){ columNum = cateList.count/4 +1; }else{ columNum = cateList.count/4; } float width = (rect_screen.size.width -20 -30)/4; channelTopicCateCell *footCell = [self getFootCell:nil]; footCell.frame = CGRectMake(0, 0, 0,width*columNum*90/150 +30); self.tableView.tableFooterView = footCell; return; }else{ self.foorView = self.tableView.tableFooterView; NSString *url = [self.topicNextUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [Remote doJsonAction:NEXTURLTAG requestUrl:url parameter:nil delegate:self]; } } - (void)setTopicNextUrl:(NSString *)url { _topicNextUrl = url; if (!isBlankString(url)) { self.nextUrl = url; } } -(void) remoteCate{ [Remote doJsonAction:CATETAG requestUrl:getTopicCategorysURL parameter:nil delegate:self]; } - (void)remoteAction { NSDictionary *dic = @{@"n":@"-3"}; [Remote doJsonAction:ALLDATATAG requestUrl:channelNodeListURL parameter:dic delegate:self]; } #pragma mark -- click top - (void)scrollTableViewToTop { [self.tableView setContentOffset:CGPointZero animated:YES]; [UIView animateWithDuration:0.3 animations:^{ CGRect tabFrame = self.createTopicBtn.frame; if (tabFrame.origin.y != CGRectGetHeight(self.view.bounds)-60) { tabFrame.origin.y = CGRectGetHeight(self.view.bounds)-60; } self.createTopicBtn.frame = tabFrame; [self.createTopicBtn setAlpha:1.0]; } completion:^(BOOL finished) { }]; } #pragma mark #pragma mark - tableView delegate and dataSource #pragma mark UITableViewDelegate and UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return dataSourceList.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [heightList[indexPath.row] floatValue]; } - (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath { id item = dataSourceList[indexPath.row]; if ([item isKindOfClass:[TopicInfoBO class]]) { return [self getContentCell:table cellForRowAtIndexPath:indexPath]; }else if ([item isKindOfClass:[listContObjectVO class]]) { return [self getBannerCell:table cellForRowAtIndexPath:indexPath]; }else { return [self getCollectionCell:table cellForRowAtIndexPath:indexPath]; } } //搜索Cell - (channelTopicSearchCell *)getSearchCell:(UITableView *)tableView { channelTopicSearchCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:topicSearchCellID]; if (nil == cell) { cell = [[channelTopicSearchCell alloc] initWithReuseIdentifier:topicSearchCellID]; } return cell; } //合集Cell - (topicCollectionCoverFlowCell *)getCollectionCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { topicCollectionCoverFlowCell *cell = [tableView dequeueReusableCellWithIdentifier:topicCollectionCoverFlowCellID]; if (nil == cell) { cell = [[topicCollectionCoverFlowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicCollectionCoverFlowCellID]; } if (refreshFlag == 1) { //bug5564: 【倒退】问吧精选页:展开收起频道导航页,问吧合集会闪一下 cell.specialBOList = dataSourceList[indexPath.row]; cell.delegate = self; refreshFlag = 0; } UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; cell.orientation = orientation; return cell; } //分类Cell - (channelTopicCateCell *)getCateCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { channelTopicCateCell *cell = [tableView dequeueReusableCellWithIdentifier:topicCateCellID]; if (nil == cell) { cell = [[channelTopicCateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicCateCellID]; } cell.cateList = cateList; cell.delegate = self; return cell; } //分类Cell - (channelTopicCateCell *)getFootCell:(UITableView *)tableView{ channelTopicCateCell *cell = [tableView dequeueReusableCellWithIdentifier:topicCateCellID]; if (nil == cell) { cell = [[channelTopicCateCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicCateCellID]; } cell.cateList = cateList; cell.delegate = self; return cell; } //问题内容 - (channelTopicContentCell *)getContentCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { channelTopicContentCell *cell = [tableView dequeueReusableCellWithIdentifier:topicContentCellID]; if (nil == cell) { cell = [[channelTopicContentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicContentCellID]; } cell.indexPath = indexPath; TopicInfoBO *topicInfo = dataSourceList[indexPath.row]; cell.topic = topicInfo; return cell; } - (channelListBannerCell*)getBannerCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellId = @"bannerCellID"; channelListBannerCell* cell = (channelListBannerCell*)[tableView dequeueReusableCellWithIdentifier:cellId]; if (nil == cell) { cell = [[channelListBannerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId]; cell.delegate = self; } cell.isTopic = YES; listContObjectVO *listBO = dataSourceList[indexPath.row]; cell.indexPath = indexPath; cell.adUrl = listBO.adUrl; CGFloat scale = (CGFloat)channelListAdScale; cell.imageScale = scale; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; if (indexPath.row > 0) { id data = dataSourceList[indexPath.row]; if ([data isKindOfClass:[NSString class]]) { if ([data isEqualToString:topicSearchCellID]) {//搜索入口 topicSeachController *vc = [topicSeachController new]; [self.navigationController pushViewController:vc animated:YES]; }else {//话题分类列表页入口 } }else if ([data isKindOfClass:[TopicInfoBO class]]){//话题详情页入口 if ([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[channelTopicContentCell class]]) { channelTopicContentCell *cell = (channelTopicContentCell*)[tableView cellForRowAtIndexPath:indexPath]; [UIView animateWithDuration:0.15 animations:^{ if (isNotIOS8) { cell.transform = CGAffineTransformMakeScale(0.97, 0.97); }else { cell.backView.transform = CGAffineTransformMakeScale(0.97, 0.97); } } completion:^(BOOL finished) { if (isNotIOS8) { cell.transform = CGAffineTransformMakeScale(1.00, 1.00); }else { cell.backView.transform = CGAffineTransformMakeScale(1.00, 1.00); } pushTopicPageWithTopicInfo(self.navigationController, data); }]; } // topicContentController *topic = [topicContentController new]; // topic.preTopicInfo = data; // [self.navigationController pushViewController:topic animated:YES]; /** * bug:5232(问吧,点击问吧卡片需要有点击效果) */ } } } #pragma mark -- set data -(void)setDataList:(NSMutableArray *)data{ _dataList = data; heightList = [NSMutableArray array]; dataSourceList = [NSMutableArray array]; // [dataSourceList addObject:topicSearchCellID]; // [heightList addObject:@"50"]; [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { CGFloat scale = 0.75; if ([obj isKindOfClass:[TopicInfoBO class]]) { TopicInfoBO *topic = obj; if (idx == 1) { topic.isFirstCard = @"1"; [dataSourceList addObject:topic]; CGFloat height = rect_screen.size.width*scale+10; [heightList addObject:[NSString stringWithFormat:@"%f",height]]; }else { topic.isFirstCard = @"0"; [dataSourceList addObject:topic]; CGFloat height = (rect_screen.size.width-20)*scale+10; [heightList addObject:[NSString stringWithFormat:@"%f",height]]; } } else if ([obj isKindOfClass:[NSString class]]) { listContObjectVO *listBO = [listContObjectVO new]; listBO.adUrl = obj; listBO.cardMode = [NSString stringWithFormat:@"%d",AdCardType]; [dataSourceList addObject:listBO]; [heightList addObject:[NSString stringWithFormat:@"%f",0.f]]; } else if ([obj isKindOfClass:[NSArray class]]) { NSMutableArray *dic = [NSMutableArray arrayWithArray:obj]; [dataSourceList addObject:dic]; if (dic && dic.count > 0) { CGFloat height = 125*(rect_screen.size.width)/300+10; [heightList addObject:[NSString stringWithFormat:@"%f",height]]; } else [heightList addObject:[NSString stringWithFormat:@"%f",0.f]]; } }]; } -(void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData{ if (ALLDATATAG== actionTag) { NSArray *list = responsData[@"topicList"]; NSMutableArray *temp = [NSMutableArray array]; self.topicCollectionList = responsData[@"banners"]; [temp addObject:self.topicCollectionList]; [list enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { if ([dic[@"cardMode"] intValue] == AdCardType) { [temp addObject:dic[@"adUrl"]]; }else { TopicInfoBO *bo = setJsonDicToDataModel(dic, [TopicInfoBO class]); [temp addObject:bo]; } }]; refreshFlag = 1; self.dataList = temp; [self.tableView reloadData]; self.topicNextUrl = responsData[@"nextUrl"]; [self endRefresh]; [self saveRemoteDataToCoreData:responsData]; }else if(NEXTURLTAG == actionTag){ NSMutableArray *oldArray = [NSMutableArray arrayWithArray:self.dataList]; NSMutableArray *tempList = responsData[@"topicList"]; [tempList enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL *stop) { if ([dic[@"cardMode"] intValue] == AdCardType) { [oldArray addObject:dic[@"adUrl"]]; }else { TopicInfoBO *bo = setJsonDicToDataModel(dic, [TopicInfoBO class]); [oldArray addObject:bo]; } }]; NSInteger firstIndex = self.dataList.count; self.dataList = [NSMutableArray arrayWithArray:oldArray]; NSMutableArray *indexList = [NSMutableArray array]; for (NSInteger i = firstIndex;i<self.dataList.count;i++) { [indexList addObject:[NSIndexPath indexPathForRow:i inSection:0]]; } [self.tableView beginUpdates]; [self.tableView insertRowsAtIndexPaths:indexList withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; [self loadSuccess]; NSString *url = responsData[@"nextUrl"]; self.topicNextUrl = url; }else if (CATETAG == actionTag){ cateList = [NSMutableArray array]; NSArray *tempCataList = responsData[@"dataList"]; [tempCataList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { categoryBO *cate = setJsonDicToDataModel(obj, [categoryBO class]); [cateList addObject:cate]; }]; } } - (void)setNodeId:(NSString *)nodeIdStr { [self manualRefresh]; _nodeId = nodeIdStr; if (self.dataList.count == 0) { if ([nodeIdStr isEqualToString:@"-1"] && ![TPUserDefault instance].userBO) { return; } NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: NSPredicate *pred = [NSPredicate predicateWithFormat:@"nodeId == %@",nodeIdStr]; [request setPredicate:pred]; ListTopicDB *listContPageDB = nil; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count > 0) { listContPageDB = [objs objectAtIndex:0]; NSDictionary *coreDataDic = makeCoreDataModelToDic(listContPageDB, [ListTopicDB class]); [self getRefreshDataFromDic:coreDataDic sourceKey:@"topicList" contentClass:[TopicInfoBO class]]; } } } - (void)getRefreshDataFromDic:(NSDictionary*)sourceDic sourceKey:(NSString*)sourceKey contentClass:(Class)contentClass{ NSArray *contList = sourceDic[sourceKey]; if (contList && contList.count > 0) { NSMutableArray *tempArray = [NSMutableArray array]; NSMutableArray *collectionList = sourceDic[@"banners"]; self.topicCollectionList = collectionList; [tempArray addObject:self.topicCollectionList]; [contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { id contentBO = setJsonDicToDataModel(obj, contentClass); if ([contentBO isKindOfClass:[listContObjectVO class]]) { listContObjectVO *list = contentBO; if ([list.cardMode intValue] != ChildListCardType) { if ([list.cardMode intValue] == HeadCardType || [list.cardMode intValue] == BigCardType || [list.cardMode intValue] == SmallCardType || [list.cardMode intValue] == HotAskCardType || [list.cardMode intValue] == AdCardType){ // if ([list.cardMode intValue] == AdCardType) { // list.adUrl = @"http://ad.thepaper.cn/s?z=paper&c=1104&op=1"; // } if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [tempArray addObject:list]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:list]; } }else { if ([Remote IsEnableWIFI]) { [tempArray addObject:list]; }else { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:list]; } } } }else if ([list.cardMode intValue] == HotTopicListType){ [tempArray addObject:list.title?list.title:@""]; [list.childList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { TopicInfoBO *topicInfoBO = setJsonDicToDataModel(obj, [TopicInfoBO class]); [tempArray addObject:topicInfoBO]; }]; } }else if ([list.cardMode intValue] == ChildListCardType){ [tempArray addObject:list.title?list.title:@""]; [list.childList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { listContObjectVO *childListBO = setJsonDicToDataModel(obj, [listContObjectVO class]); childListBO.isChildList = @"1"; if ([[TPUserDefault instance].readModeStr intValue] == imageMode) { [tempArray addObject:childListBO]; }else if ([[TPUserDefault instance].readModeStr intValue] == textMode) { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:childListBO]; } }else { if ([Remote IsEnableWIFI]) { [tempArray addObject:childListBO]; }else { if (![list.forwordType intValue] == imageNewsForwardType) { [tempArray addObject:childListBO]; } } } }]; } }else { [tempArray addObject:contentBO]; } }]; refreshFlag = 1; self.dataList = tempArray; [self.tableView reloadData]; NSString *url = sourceDic[@"nextUrl"]; self.nextUrl = url; }else { self.dataList = [NSMutableArray array]; [self.tableView reloadData]; TPLOG(@"话题列表为空"); } } - (void)saveRemoteDataToCoreData:(id)resData { NSError *error; NSFetchRequest *request = [[NSFetchRequest alloc]init]; //为已创建好的实体利用检索到的上下文创建一个实体描述 NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; [request setEntity:entityDescription]; //确定持久库中是否存在与此字段相对应的托管对象,所以穿件一个谓词来确定字段的正确对象: NSPredicate *pred = [NSPredicate predicateWithFormat:@"nodeId == %@",@"-3"]; [request setPredicate:pred]; ListTopicDB *listContPageDB = nil; NSArray *objs = [[CoreDataManager shareInstance].managedObjectContext executeFetchRequest:request error:&error]; if (objs == nil) { TPLOG(@"there was an error!!"); } if (objs.count > 0) { listContPageDB = [objs objectAtIndex:0]; [[CoreDataManager shareInstance].managedObjectContext deleteObject:listContPageDB]; } ListTopicDB *listDB = nil; listDB = [NSEntityDescription insertNewObjectForEntityForName:@"ListTopicDB" inManagedObjectContext:[CoreDataManager shareInstance].managedObjectContext]; listDB = makeDicToCoreDataModel(resData, listDB); listDB.date = getLocalDate(); listDB.nodeId = @"-3"; [[CoreDataManager shareInstance] saveContext]; } -(void)returnCate:(categoryBO *)cate index:(NSInteger)index{//问吧分类 InterViewPageViewController *vc = [InterViewPageViewController new]; vc.cateBo = cate; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - banner delegate - (void)loadAdSuccess:(NSIndexPath *)indexPath isHeadCell:(BOOL)isHeadCell { listContObjectVO *listBO = dataSourceList[indexPath.row]; if ([listBO.isLoadAdSuccess intValue] == 1) { return; } listBO.isLoadAdSuccess = @"1"; [dataSourceList replaceObjectAtIndex:indexPath.row withObject:listBO]; CGFloat cellHeight = (rect_screen.size.width-20)*channelListAdScale+10; [heightList replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"%f",cellHeight]]; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; } - (void)loadAdFailed:(NSIndexPath *)indexPath isHeadCell:(BOOL)isHeadCell { } - (void)clickAdd:(remotePushBO *)pushBO indexPath:(NSIndexPath *)indexPath{ bannerCell *cell = (bannerCell*)[self.tableView cellForRowAtIndexPath:indexPath]; [UIView animateWithDuration:0.15 animations:^{ cell.bannerView.transform = CGAffineTransformMakeScale(0.97, 0.97); } completion:^(BOOL finished) { cell.bannerView.transform = CGAffineTransformMakeScale(1.00, 1.00); [TPUserDefault instance].pushBO = pushBO; }]; } -(void) gotoSearchSelector:(UIButton *) btn{ topicSeachController *vc = [topicSeachController new]; [self.navigationController pushViewController:vc animated:YES]; } - (void)pushToCoverFlowDetail:(specialObjectBO*)specialBO { topicCollectionController *topicCollectionVC = [topicCollectionController new]; topicCollectionVC.nodeId = specialBO.nodeId; topicCollectionVC.name = specialBO.name; [self.navigationController pushViewController:topicCollectionVC animated:YES]; } - (void)createTopicBtnSelector { if ([TPUserDefault instance].userBO) { creatTopicController *createTopicVC = [[creatTopicController alloc] init]; [self.navigationController pushViewController:createTopicVC animated:YES]; }else{ //bug5581: 问吧:未登录用户点击创建话题按钮应跳转到登录界面 loginHomeController *loginVC = [[loginHomeController alloc]init]; [self.navigationController pushViewController:loginVC animated:YES]; } } //问吧:切换无图模式,不能立即生效(bug:5041) - (void) reachabilityChanged:(NSNotification *)note { self.dataList = [NSMutableArray arrayWithArray:self.dataList]; [self.tableView reloadData]; } - (void)needRefreshReadMode:(id)sender { self.dataList = [NSMutableArray arrayWithArray:self.dataList]; [self.tableView reloadData]; } - (void)reloadWithFontChange { self.dataList = [NSMutableArray arrayWithArray:self.dataList]; [self.tableView reloadData]; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end