工具栏中的Admob UITableViewController Xcode

时间:2016-12-17 16:21:29

标签: ios objective-c xcode admob toolbar

我正在使用objective-c在Xcode中开发一个应用程序。我的问题是我正在尝试使用ToolBar在我的UITableViewController中显示admob广告,但这个栏没有显示任何内容。我在网上发现了很多关于如何将这个ToolBar放在UITableViewController中的回复,但是我的情况并没有显示任何内容。我知道我的Xcode水平很低,但我每天都在努力改进。

这是我对admob的代码:

[self.navigationController setToolbarHidden:NO];
    self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
    self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; //TEST!!!!!
    self.bannerView.rootViewController = self;
    [self.navigationController.toolbar addSubview:self.bannerView];

    GADRequest *request = [GADRequest request];
    request.testDevices = @[ @"",];
    [self.bannerView loadRequest:request];

这是我的MainTableViewController.h:

#import <UIKit/UIKit.h>
#import "Restaurant.h"

@import GoogleMobileAds;
@interface MainTableViewController : UITableViewController
@property (weak, nonatomic) IBOutlet UIBarButtonItem *barButton;
@property (nonatomic, strong) NSArray<Restaurant *> *originData;
@property (nonatomic, strong) NSMutableArray<Restaurant *> *filteredRest;
@property (nonatomic, assign) Boolean isFiltered;
@property (weak, nonatomic) IBOutlet UISearchBar *mySearchBar;
@property (strong, nonatomic) IBOutlet UITableView *RestTableView;
@property (weak, nonatomic) IBOutlet UIToolbar *admobToolBar;
@property(weak, nonatomic) IBOutlet GADBannerView *bannerView;

@end

这是我的MainTableviewController.m:

    #import "MainTableViewController.h"
    #import "SWRevealViewController.h"
    #import "RestTableViewCell.h"
    #import "RestViewController.h"
    #import "Restaurant.h"

    @interface MainTableViewController ()
    @end
    @implementation MainTableViewController

    @synthesize mySearchBar, filteredRest, isFiltered, originData;

    - (void)viewDidLoad {
        [super viewDidLoad];

        _barButton.target = self.revealViewController;
        _barButton.action = @selector(revealToggle:);

        [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];

        self.RestTableView.tableFooterView = [[UIView alloc] init]; /*Esta linea hace que en la tabla solo aparezcan el numero de filas que tienes establecidas, es decir, que las vacias no aparezcan*/



        [self.navigationController setToolbarHidden:NO];
        self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
        self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716"; //TEST!!!!!
        //self.bannerView.adUnitID = @"ca-app-pub-6926442062866079/7221537141";
        self.bannerView.rootViewController = self;
        [self.navigationController.toolbar addSubview:self.bannerView]; 
        GADRequest *request = [GADRequest request];
        request.testDevices = @[ @"",];
        [self.bannerView loadRequest:request];

        originData = @[
            [[Restaurant alloc] init:@"80 Grados" descripiton:@"Malasaña" image:@"80_grados.jpg"],
            [[Restaurant alloc] init:@"90 Grados" descripiton:@"Retiro" image:@"90_grados.jpg"],
            [[Restaurant alloc] init:@"B&B Babel" descripiton:@"Barrio de Chueca" image:@"babel.jpg"],
            [[Restaurant alloc] init:@"Babelia" descripiton:@"Barrio de Salamanca" image:@"babelia.jpg"],
            [[Restaurant alloc] init:@"Bacira" descripiton:@"Chamberí" image:@"bacira.jpg"],
            [[Restaurant alloc] init:@"Bar Galleta" descripiton:@"Malasaña" image:@"bar_galleta.jpg"],
            [[Restaurant alloc] init:@"Bar Tomate" descripiton:@"Chamberí" image:@"bar_tomate.jpg"],

        filteredRest = [NSMutableArray new];
        isFiltered = NO;

    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    { 
        // Return the number of sections.
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        if (isFiltered == YES) {
            return filteredRest.count;
        } else {
            return originData.count;
        }
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"TableCell";
        RestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

        // Configure the cell...
        if (isFiltered == YES) {
            cell.TitleLabel.text = [filteredRest objectAtIndex:indexPath.row].title;
            cell.DescriptionLabel.text = [filteredRest objectAtIndex:indexPath.row].desc;
            cell.RestImage.image = [UIImage imageNamed:[filteredRest objectAtIndex:indexPath.row].image];
        } else {
            cell.TitleLabel.text = [originData objectAtIndex:indexPath.row].title;
            cell.DescriptionLabel.text = [originData objectAtIndex:indexPath.row].desc;
            cell.RestImage.image = [UIImage imageNamed:[originData objectAtIndex:indexPath.row].image];
        }

        cell.RestImage.layer.cornerRadius = 6;
        cell.RestImage.clipsToBounds = YES;
        cell.RestImage.layer.borderWidth = 1;

        return cell;
    }


    -(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

        if ([[segue identifier] isEqualToString:@"ShowDetails"]){
            RestViewController *restviewcontroller = [segue destinationViewController];

            NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];

            if (isFiltered) {
                restviewcontroller.DetailModal = filteredRest[myIndexPath.row];
            } else {
                restviewcontroller.DetailModal = originData[myIndexPath.row];
            }
        }
    }

@end

ToolBar没有显示任何内容:

enter image description here

有什么不对? 我需要你的帮助!非常感谢您的回复!

1 个答案:

答案 0 :(得分:0)

您是否有任何理由使用UIToolBar来展示此广告?

我建议使用UIViewController而不是使用UITableViewController,并在那里为UITableView创建一个属性。

完成后,您可以设置约束:

=====

的tableview

广告视图(设置高度)

==

使用带有表视图的UIViewController的好处是你可以在屏幕上放置任何其他东西,而不仅仅是tableview:)

我希望有所帮助!如果您需要更多关于从哪里开始的信息,请随时回复!