//
|
// discoverView.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou on 16/8/1.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "discoverView.h"
|
#import "CellOne.h"
|
#import "CellHead.h"
|
#import "WebControllerView.h"
|
#import "TWScontroller.h"
|
#import "StarsController.h"
|
|
#import "findGoodsTableViewCell.h"
|
#import "OnlySpecialController.h"
|
#import "IndividualStarController.h"
|
#import "discoverGoodsDetailViewController.h"
|
#import "allCommentsViewController.h"
|
#import "DisCoverTableViewCell.h"
|
#import "DisCoverADView.h"
|
//#import "GDTNativeAd.h"//广点通原生广告
|
#import "GDTNativeExpressAd.h"
|
#import "GDTNativeExpressAdView.h"
|
|
@interface discoverView()<UITableViewDataSource,UITableViewDelegate ,GDTNativeExpressAdDelegete>{
|
UILabel * SDYname; //专题换的文字
|
int goodslist_page; //请求发现页面的商品页数
|
// GDTNativeAd *_nativeAd; //原生广告实例
|
// NSArray *nativeArray;//存储请求下来的原生广告信息
|
|
}
|
|
@property(nonatomic,strong) UITableView *mytableview;//发现页的主视图
|
@property(nonatomic,strong) NSMutableArray *dataSDY;//专题数据
|
@property(nonatomic,strong) NSMutableArray *dataGoodsList;//商品列表
|
@property (nonatomic, strong) NSArray *expressAdViews;
|
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
|
@end
|
|
@implementation discoverView
|
|
- (instancetype)initWithFrame:(CGRect)frame {
|
self = [super initWithFrame:frame];
|
if (self) {
|
// _mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) ];
|
_mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStyleGrouped];
|
_mytableview.delegate = self;
|
_mytableview.dataSource = self;
|
_mytableview.backgroundColor = kGlobalBackgroundColor;
|
_mytableview.separatorStyle = UITableViewCellSeparatorStyleNone;
|
_mytableview.showsVerticalScrollIndicator = NO;
|
_mytableview.estimatedRowHeight = 0;
|
_mytableview.estimatedSectionFooterHeight = 0;
|
_mytableview.estimatedSectionHeaderHeight=0 ;
|
//默认请求第一页的数据
|
goodslist_page=1;
|
_mytableview.rowHeight = KScreenW * 0.437 + 50;
|
//注册cell
|
[_mytableview registerNib:[UINib nibWithNibName:@"CellOne" bundle:nil] forCellReuseIdentifier:@"CellOne"];
|
[_mytableview registerNib:[UINib nibWithNibName:@"findGoodsTableViewCell" bundle:nil] forCellReuseIdentifier:@"findGoodsTableViewCell"];
|
[_mytableview registerNib:[UINib nibWithNibName:@"DisCoverTableViewCell" bundle:nil] forCellReuseIdentifier:@"DisCoverTableViewCell"];
|
|
|
_mytableview.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNetData)];
|
|
|
_mytableview .mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
|
[self loadFoundHomeBanner];
|
}];
|
//将tableView添加到发现页面的视图
|
[self addSubview:_mytableview];
|
[self loadAd];
|
|
//预加载数据
|
[self PreLoad];
|
|
}
|
return self;
|
}
|
|
//懒加载发现页面的数据
|
-(void)PreLoad{
|
//1.获取旧的发现页面轮播图页面
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
//2.判断本地是否有轮播图数据,如果有数据就进行预加载
|
if ([fileManager fileExistsAtPath:DISCOVERSDY]){
|
_dataSDY=[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:DISCOVERSDY]];
|
[_mytableview reloadData];
|
}
|
//3.请求发现页面的网络数据
|
[self loadNetData];
|
}
|
|
/**
|
加载网络数据
|
*/
|
-(void)loadNetData{
|
goodslist_page=1;
|
[self loadFoundHomeBanner];
|
[self loadGoodsItemList];
|
// [self loadAd];
|
}
|
|
/**
|
请求轮播图数据
|
*/
|
-(void)loadFoundHomeBanner{
|
[[YTHNetInterface startInterface] getFoundHomeBanner:[YTHsharedManger startManger].Uid WithSystem:@"1" WithBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
if (!_dataSDY) {
|
_dataSDY=[[NSMutableArray alloc] initWithCapacity:0];
|
}else{
|
//清空之前的数据
|
[_dataSDY removeAllObjects];
|
}
|
_dataSDY = [[result objectForKey:@"Data"] objectForKey:@"data"];
|
[_mytableview.mj_footer endRefreshing];
|
if (_dataSDY.count < 20) {
|
[_mytableview.mj_footer endRefreshingWithNoMoreData];
|
}
|
NSLog(@"%@",_dataSDY);
|
//存储获取到的数据
|
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_dataSDY];
|
[data writeToFile:DISCOVERSDY atomically:YES];
|
[_mytableview reloadData];
|
}else{
|
NSLog(@"%@",error);
|
}
|
[_mytableview.mj_header endRefreshing];
|
}];
|
}
|
|
/**
|
请求商品列表数据
|
*/
|
-(void)loadGoodsItemList{
|
[[YTHNetInterface startInterface] getGoodsItemListWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withPage:[NSString stringWithFormat:@"%d",goodslist_page] withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if(isSuccessful){
|
if (!_dataGoodsList) {
|
_dataGoodsList=[[NSMutableArray alloc] initWithCapacity:0];
|
}
|
if(goodslist_page==1){
|
[_dataGoodsList removeAllObjects];
|
}
|
NSArray *arr=[[result objectForKey:@"Data"] objectForKey:@"data"];
|
[_mytableview.mj_header endRefreshing];
|
[_mytableview.mj_footer endRefreshing];
|
if ([arr count]>0) {
|
++goodslist_page;
|
[_dataGoodsList addObjectsFromArray:arr];
|
[_mytableview reloadData];
|
}else{
|
[_mytableview.mj_footer endRefreshingWithNoMoreData];
|
}
|
[self loadAd];
|
|
}else{
|
NSLog(@"%@",error);
|
[_mytableview.mj_header endRefreshing];
|
[_mytableview.mj_footer endRefreshing];
|
}
|
|
}];
|
}
|
|
/**
|
点击收藏按钮
|
|
@param sender 按钮
|
*/
|
-(void)clickCollectionBtn:(UIButton *)sender{
|
if([sender isSelected]){
|
sender.selected=NO;
|
NSString *goodssId=[NSString stringWithFormat:@"%d",[[_dataGoodsList[sender.tag-100] objectForKey:@"Id"] intValue]];
|
[[YTHNetInterface startInterface] cancelCollectWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:goodssId withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
if ([result objectForKey:@"IsPost"]) {
|
sender.selected=NO;
|
NSNumber *boolNumber = [NSNumber numberWithBool:NO];
|
[_dataGoodsList[sender.tag-100] setValue:boolNumber forKey:@"Collect"];
|
}else{
|
sender.selected=YES;
|
}
|
}else{
|
sender.selected=YES;
|
}
|
}];
|
}else{
|
sender.selected=YES;
|
NSString *goodssId=[NSString stringWithFormat:@"%d",[[_dataGoodsList[sender.tag-100] objectForKey:@"Id"] intValue]];
|
[[YTHNetInterface startInterface] addCollectWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" withId:goodssId withBlock:^(BOOL isSuccessful, id result, NSString *error) {
|
if (isSuccessful) {
|
if ([result objectForKey:@"IsPost"]) {
|
sender.selected=YES;
|
NSNumber *boolNumber = [NSNumber numberWithBool:YES];
|
[_dataGoodsList[sender.tag-100] setValue:boolNumber forKey:@"Collect"];
|
}else{
|
sender.selected=NO;
|
}
|
}else{
|
sender.selected=NO;
|
}
|
}];
|
}
|
}
|
|
/**
|
跳转到评论页面
|
@param sender 按钮
|
*/
|
-(void)clickCommitBtn:(UIButton *)sender{
|
NSLog(@"%ld",(long)sender.tag);
|
allCommentsViewController *allCommentVC=[[allCommentsViewController alloc] init];
|
allCommentVC.goodsId=[_dataGoodsList[sender.tag-100] objectForKey:@"Id"];
|
[allCommentVC setHidesBottomBarWhenPushed:YES];
|
[[self viewController].navigationController pushViewController:allCommentVC animated:YES];
|
}
|
|
#pragma mark UITableViewDataSource
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
|
return self.dataSDY.count;
|
}
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
// if(section==1){
|
// return _dataGoodsList.count;
|
// }else{
|
// return 1;
|
// }
|
return 1;
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
DisCoverTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DisCoverTableViewCell"];
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
|
for (int i = 0; i < _dataSDY.count; ++i) {
|
[arr addObject:[_dataSDY[i] objectForKey:@"Picture"]];
|
NSLog(@"%@",_dataSDY[i]);
|
}
|
NSLog(@"%lu",(unsigned long)arr.count);
|
[cell.pic setYthImageWithURL:[_dataSDY[indexPath.section] objectForKey:@"Picture"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
|
cell.nameLabel.text = [NSString stringWithFormat:@"%@",[_dataSDY[indexPath.section] objectForKey:@"Name"]];
|
|
return cell;
|
|
}
|
|
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
|
// if (section == 1){
|
// CellHead * head = [CellHead getCellHead:CGRectMake(0, 0, self.frame.size.width, 50)];
|
// head.image.image = [UIImage imageNamed:@"发现东西"];
|
// head.name.text = @"发现东西";
|
// head.backgroundColor = kGlobalBackgroundColor;
|
// return head;
|
// }
|
// return nil;
|
//}
|
|
#pragma mark UITableViewDelegate
|
|
|
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
|
|
if (section % 5 == 0 && self.expressAdViews.count != 0) {
|
NSInteger infoIndex;
|
if (self.expressAdViews.count <= (section / 5)) {
|
infoIndex = (section/5)%self.expressAdViews.count;
|
}else{
|
infoIndex = section / 5;
|
}
|
UIView *view = self.expressAdViews[infoIndex];
|
return view.bounds.size.height + 10 ;
|
}else{
|
return CGFLOAT_MIN;
|
}
|
}
|
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
|
if (section % 5 == 0 && self.expressAdViews.count != 0) {
|
|
NSInteger infoIndex;
|
if (self.expressAdViews.count <= (section / 5)) {
|
infoIndex = (section/5)%self.expressAdViews.count;
|
}else{
|
infoIndex = section / 5;
|
}
|
|
GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.expressAdViews[infoIndex];
|
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, adView.bounds.size.height + 10 )];
|
view.backgroundColor = kGlobalBackgroundColor;
|
[view addSubview:adView];
|
return view;
|
}else{
|
UIView *view = [[UIView alloc]init];
|
return view;
|
}
|
}
|
/**
|
* 点击广点通广告的方法
|
*/
|
//-(void)getMoreInformation:(UIButton *)sender{
|
// GDTNativeAdData * info;
|
// NSInteger infoIndex = sender.tag - 456;
|
// info=nativeArray[infoIndex];
|
// [_nativeAd clickAd:info];
|
//
|
//}
|
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
|
//
|
// nativeArray = nativeAdDataArray;
|
// [self.mytableview reloadData];
|
//
|
//}
|
//- (void)nativeAdFailToLoad:(NSError *)error{
|
// NSLog(@"%@",error);
|
//}
|
/**
|
* 加载广点通原生广告
|
*/
|
-(void)loadAd{
|
// _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey1];
|
// _nativeAd.controller = [self topViewController];
|
// _nativeAd.delegate = self;
|
// [_nativeAd loadAd:10];
|
|
// 支持视频广告的 PlacementId 会混出视频与图片广告
|
self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithPlacementId:GDTYSADkey1 adSize:CGSizeMake(KScreenW, (KScreenW-20)/16*9 + 10)];
|
|
|
self.nativeExpressAd.delegate = self;
|
// 配置视频播放属性
|
self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
|
self.nativeExpressAd.videoMuted = NO;
|
[self.nativeExpressAd loadAd:10];
|
}
|
- (void)nativeExpressAdSuccessToLoad:(GDTNativeExpressAd
|
*)nativeExpressAd views:(NSArray<__kindof
|
GDTNativeExpressAdView *> *)views
|
{
|
self.expressAdViews = [NSArray arrayWithArray:views];
|
if (self.expressAdViews.count) {
|
[self.expressAdViews enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
GDTNativeExpressAdView *expressView = (GDTNativeExpressAdView *)obj;
|
expressView.controller = [self topViewController];
|
[expressView render];
|
}];
|
}
|
// 广告位 render 后刷新 tableView
|
[self.mytableview reloadData];
|
}
|
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
|
NSLog(@"%@",error);
|
}
|
|
- (UIViewController *)_topViewController:(UIViewController *)vc {
|
if ([vc isKindOfClass:[UINavigationController class]]) {
|
return [self _topViewController:[(UINavigationController *)vc topViewController]];
|
} else if ([vc isKindOfClass:[UITabBarController class]]) {
|
return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
|
} else {
|
return vc;
|
}
|
return nil;
|
}
|
- (UIViewController *)topViewController {
|
|
UIViewController *resultVC;
|
resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
|
while (resultVC.presentedViewController) {
|
resultVC = [self _topViewController:resultVC.presentedViewController];
|
}
|
return resultVC;
|
}
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
|
return CGFLOAT_MIN;
|
}
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
|
[tableView deselectRowAtIndexPath:indexPath animated:NO];
|
|
NSDictionary *dic =_dataSDY[indexPath.section];
|
if([[dic allKeys] containsObject:@"CommonAd"]){ //跳转网页
|
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[dic objectForKey:@"Link"]]];
|
}else{ //跳转内部
|
OnlySpecialController * oscontroller = [OnlySpecialController new];
|
oscontroller.Id = ((NSString*)[dic objectForKey:@"Id"]).intValue;
|
[oscontroller setHidesBottomBarWhenPushed:YES];
|
[[self viewController].navigationController pushViewController:oscontroller animated:YES];
|
}
|
}
|
|
|
|
/**
|
* 返回当前视图的控制器
|
*/
|
- (UIViewController *)viewController {
|
for (UIView* next = [self superview]; next; next = next.superview) {
|
UIResponder *nextResponder = [next nextResponder];
|
if ([nextResponder isKindOfClass:[UIViewController class]]) {
|
return (UIViewController *)nextResponder;
|
}
|
}
|
return nil;
|
}
|
|
|
@end
|