重庆迈尖科技有限公司
2019-05-08 8c116459f2308e9f8564ea8f55b2ca25849b8336
BuWanVideo2.0/searchDetailViewController.m
@@ -12,13 +12,20 @@
#import "XYRDetailViewController.h"
#import "XYRVideoInfoModel.h"
//#import "GDTNativeAd.h"
#import "DisCoverADView.h"
#import "GDTNativeExpressAd.h"
#import "GDTNativeExpressAdView.h"
static NSString *indentfly = @"cell";
static NSString *indentfly2=@"searchTableViewCellID";
@interface searchDetailViewController ()<YTHSearchTextFieldDelegate,UITableViewDataSource,UITableViewDelegate>{
@interface searchDetailViewController ()<YTHSearchTextFieldDelegate,UITableViewDataSource,UITableViewDelegate,GDTNativeExpressAdDelegete>{
    NSInteger pagenumber;
    NSInteger typenumber;
    //广点通原生广告
//    GDTNativeAd *_nativeAd;     //原生广告实例
//    NSArray  *nativeArray;//存储请求下来的原生广告信息
}
@property (nonatomic , strong) YTHSearchTextField *searchField;//搜索框对象
@property (nonatomic , strong) NSMutableArray *dataSearch;//搜索结果数据
@@ -29,6 +36,8 @@
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (nonatomic , strong) UITableView *DisplayTabelView;//用于显示搜索结果的列表
@property (nonatomic, strong) NSArray *expressAdViews;
@property (nonatomic, strong) GDTNativeExpressAd *nativeExpressAd;
@end
@implementation searchDetailViewController
@@ -45,6 +54,8 @@
    [self addSegment];
    //添加搜索结果的列表
    [self addTabelView];
    [self loadAd];
}
- (void)viewWillAppear:(BOOL)animated
@@ -101,6 +112,8 @@
    
    //添加搜索按钮
    self.navigationItem.rightBarButtonItem=searchItem;
    self.view.backgroundColor = kGlobalBackgroundColor;
}
/**
 *  添加segment的属性
@@ -115,6 +128,57 @@
    [self.segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
}
/**
 *  加载广点通原生广告
 */
-(void)loadAd{
//    _nativeAd = [[GDTNativeAd alloc]initWithAppId: GDTADkey placementId:GDTYSADkey3];
//    _nativeAd.controller = self;
//    _nativeAd.delegate = self;
//    [_nativeAd loadAd:10];
    self.nativeExpressAd = [[GDTNativeExpressAd alloc] initWithAppId:GDTADkey placementId:GDTYSADkey7 adSize:CGSizeMake(KScreenW, 95)];
    self.nativeExpressAd.delegate = self;
    // 配置视频播放属性
    self.nativeExpressAd.videoAutoPlayOnWWAN = YES;
    self.nativeExpressAd.videoMuted = NO;
    [self.nativeExpressAd loadAd:10];
}
//- (void)getMoreInformation:(UIButton *)sender{
//    GDTNativeAdData * info =nativeArray[sender.tag - 456];
//    [_nativeAd clickAd:info];
//}
#pragma mark -GDTNativeAdDelegate
- (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;
            [expressView render];
        }];
    }
    // 广告位 render 后刷新 tableView
    [self.DisplayTabelView reloadData];
}
- (void)nativeExpressAdFailToLoad:(GDTNativeExpressAd *)nativeExpressAd error:(NSError *)error{
    NSLog(@"%@",error);
}
///**
// *  原生广告加载广告数据成功回调,返回为GDTNativeAdData对象的数组
// */
//-(void)nativeAdSuccessToLoad:(NSArray *)nativeAdDataArray{
//    nativeArray = nativeAdDataArray;
//    [_DisplayTabelView reloadData];
//}
//
///**
// *  原生广告加载广告数据失败回调
// */
//-(void)nativeAdFailToLoad:(NSError *)error{
//    NSLog(@"XYR_%@",error);
//}
/**
 *  添加用于显示的列表
 */
-(void)addTabelView{
@@ -124,12 +188,16 @@
            height = 94;
        }
        
        self.DisplayTabelView=[[UITableView alloc] initWithFrame:CGRectMake(0, height+40, KScreenW, KScreenH-height-40) style:UITableViewStylePlain];
        self.DisplayTabelView=[[UITableView alloc] initWithFrame:CGRectMake(0, height+40, KScreenW, KScreenH-height-40) style:UITableViewStyleGrouped];
    }
    self.DisplayTabelView.delegate=self;
    self.DisplayTabelView.dataSource=self;
    NSLog(@"%f",KScreenH);
    self.DisplayTabelView.rowHeight = 95.0;
    //tableView背景色
    self.DisplayTabelView.backgroundColor=kGlobalBackgroundColor;
    self.DisplayTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册Cell
    [self.DisplayTabelView registerNib:[UINib nibWithNibName:@"searchTableViewCell" bundle:nil] forCellReuseIdentifier:indentfly2];
    [self.view addSubview:self.DisplayTabelView];
@@ -225,6 +293,7 @@
    suggestTableview.delegate = self;
    suggestTableview.dataSource = self;
    suggestTableview.hidden = YES;
    suggestTableview.rowHeight = 44;
    _suggestTableview = suggestTableview;
    [self.view addSubview:suggestTableview];
}
@@ -263,6 +332,7 @@
 *  @param page       搜索的页数
 */
- (void)searchDataWithText:(NSString *)searchText withType:(NSInteger)type withPage:(NSInteger )page{
    [self loadAd];
    [[YTHNetInterface startInterface] getSerachWithUid:[YTHsharedManger startManger].Uid withKey:searchText withType:[NSString stringWithFormat:@"%ld",(long)type] withVideoType:[NSString stringWithFormat:@"%ld",(long)type] withSystem:@"1" withPage:[NSString stringWithFormat:@"%ld",(long)page] withblock:^(BOOL isSuccessful, id result, NSString *error) {
        if (isSuccessful) {
            NSDictionary *dic = (NSDictionary *)result;
@@ -371,27 +441,32 @@
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView==self.suggestTableview) {
        return _suggestSearch.count;
    }else{
        return _dataSearch.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.suggestTableview) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfly];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentfly];
        }
        cell.textLabel.text = [_suggestSearch objectAtIndex:indexPath.row];
        cell.textLabel.text = [_suggestSearch objectAtIndex:indexPath.section];
        return cell;
    }else{
        searchTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentfly2];
        cell.clickBtn.hidden = YES;
        cell.ADImage.hidden = YES;
        //获取到该cell的数据
        XYRVideoInfoModel *model = [XYRVideoInfoModel yy_modelWithDictionary:[_dataSearch objectAtIndex:indexPath.row]];
        XYRVideoInfoModel *model = [XYRVideoInfoModel yy_modelWithDictionary:[_dataSearch objectAtIndex:indexPath.section]];
        //加载图片
        [cell.searchTableViewCellImage setYthImageWithURL:model.Hpicture placeholderImage:[UIImage imageNamed:@"默认加载图片"]];
        NSLog(@"%@",cell.searchTableViewCellImage);
        //加载名称
        cell.searchTableViewCellTitle.text=model.Name;
        //显示Tag值
@@ -399,6 +474,7 @@
        cell.SearchTabelViewTag.textColor=[UIColor grayColor];
        //观看数量
        cell.searchTableViewCellWatchNUB.textColor=kGlobalLightGreyColor_210;
        NSString *recommentStr;
        if (model.WatchCount==nil){
            recommentStr=[NSString stringWithFormat:@"0"];
@@ -418,31 +494,69 @@
            commentCountStr=[NSString stringWithFormat:@"%0.1f万",[commentCountStr floatValue]/10000.0];
        }
        cell.searchTableViewCellCommentNUB.text=[NSString stringWithFormat:@"%@",commentCountStr];
        cell.backgroundColor = kGlobalBackgroundColor;
        return cell;
    }
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView==self.suggestTableview){
        return 44;
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 0.5)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section % 20 == 0 && self.expressAdViews.count != 0) {
        NSInteger infoIndex;
        if (self.expressAdViews.count <= (section / 20)) {
            infoIndex = (section/20)%self.expressAdViews.count;
        }else{
            infoIndex = section / 20;
        }
        GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.expressAdViews[infoIndex];
        return adView.bounds.size.height - 4;
    }else{
        return 95;
        return CGFLOAT_MIN;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section %20 == 0 && self.expressAdViews.count != 0) {
        NSInteger infoIndex;
        if (self.expressAdViews.count <= (section / 20)) {
            infoIndex = (section/20)%self.expressAdViews.count;
        }else{
            infoIndex = section / 20;
        }
        GDTNativeExpressAdView *adView = (GDTNativeExpressAdView *)self.expressAdViews[infoIndex];
        adView.backgroundColor = kGlobalBackgroundColor;
        return adView;
    }else{
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 0)];
        return view;
    }
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if(tableView==self.suggestTableview){
//        return 44;
//    }else{
//        return 95;
//    }
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(tableView==self.suggestTableview){
        _searchField.Field.text = [_suggestSearch objectAtIndex:indexPath.row];
        _searchField.Field.text = [_suggestSearch objectAtIndex:indexPath.section];
        [self searchCotol:_searchField.Field.text];
        [self.DisplayTabelView.mj_header beginRefreshing];
    }else{
        XYRDetailViewController *detailVC=[[XYRDetailViewController alloc] init];
        detailVC.Model=[XYRVideoInfoModel yy_modelWithDictionary:_dataSearch[indexPath.row]];
        detailVC.Model=[XYRVideoInfoModel yy_modelWithDictionary:_dataSearch[indexPath.section]];
        [self presentViewController:detailVC animated:YES completion:^{
            
        }];