重庆迈尖科技有限公司
2019-05-08 8c116459f2308e9f8564ea8f55b2ca25849b8336
BuWanVideo2.0/discoverView.m
@@ -19,15 +19,24 @@
#import "discoverGoodsDetailViewController.h"
#import "allCommentsViewController.h"
#import "DisCoverTableViewCell.h"
@interface discoverView()<UITableViewDataSource,UITableViewDelegate>{
#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
@@ -49,10 +58,9 @@
        _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"];
@@ -67,8 +75,11 @@
        }];
        //将tableView添加到发现页面的视图
        [self addSubview:_mytableview];
        [self loadAd];
        //预加载数据
        [self PreLoad];
    }
    return self;
}
@@ -94,6 +105,7 @@
    goodslist_page=1;
    [self loadFoundHomeBanner];
    [self loadGoodsItemList];
//    [self loadAd];
}
/**
@@ -137,7 +149,6 @@
            if(goodslist_page==1){
                [_dataGoodsList removeAllObjects];
            }
            NSArray *arr=[[result objectForKey:@"Data"] objectForKey:@"data"];
            [_mytableview.mj_header endRefreshing];
            [_mytableview.mj_footer endRefreshing];
@@ -148,6 +159,8 @@
            }else{
                [_mytableview.mj_footer endRefreshingWithNoMoreData];
            }
            [self loadAd];
        }else{
            NSLog(@"%@",error);
            [_mytableview.mj_header endRefreshing];
@@ -212,7 +225,7 @@
#pragma mark  UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    return self.dataSDY.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
@@ -221,7 +234,7 @@
//    }else{
//        return 1;
//    }
    return self.dataSDY.count;
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
@@ -233,8 +246,8 @@
        NSLog(@"%@",_dataSDY[i]);
    }
    NSLog(@"%lu",(unsigned long)arr.count);
    [cell.pic setYthImageWithURL:[_dataSDY[indexPath.row] objectForKey:@"Picture"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[_dataSDY[indexPath.row] objectForKey:@"Name"]];
    [cell.pic setYthImageWithURL:[_dataSDY[indexPath.section] objectForKey:@"Picture"] placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",[_dataSDY[indexPath.section] objectForKey:@"Name"]];
    return cell;
@@ -252,19 +265,116 @@
//}
#pragma mark  UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (indexPath.section == 0) {
//        return [CellOne getHeight] + 20;
    return KScreenW * 0.437 + 50;
//    }else if(indexPath.section ==1){
//        return KScreenW+175;
//    }
//    return 0.1;
}
-(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];
    return CGFLOAT_MIN;
    // 支持视频广告的 PlacementId 会混出视频与图片广告
    self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithAppId:GDTADkey placementId: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;
@@ -273,7 +383,7 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *dic =_dataSDY[indexPath.row];
    NSDictionary *dic =_dataSDY[indexPath.section];
    if([[dic allKeys] containsObject:@"CommonAd"]){  //跳转网页
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[dic objectForKey:@"Link"]]];
    }else{  //跳转内部
@@ -298,4 +408,6 @@
    }
    return nil;
}
@end