UITableView GADBannerView
I am following the Google AdMob Ads iOS tutorial to get Ads working. Everything works well until i try to add Ads to a UITableView. My design was to have two sections on a Table where the Ad would appear on the first section and the table data on the second section. This however does not work too well as i get the ad in the first section BUT it is also repeated every 10th cell. I only want the Ad once. How do i do this.
Here is my code...
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *refreshButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)];
self.navigationItem.rightBarButtonItem = refreshButtonItem;
[refreshButtonItem release];
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
开发者_如何学C0.0,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = @"blablabla";
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
GADRequest *adMobRequest = [GADRequest request];
adMobRequest.testDevices = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID, // Simulator
@"fafasfasdfasdrasdasfasfaasdsd", nil];
// Initiate a generic request to load it with an ad.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
if (section == 0) {
return 1;
} else {
return 50;
}
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (indexPath.section == 0) {
if (indexPath.row == 0) {
[cell addSubview:bannerView_];
}
} else {
cell.textLabel.text = @"Test";
cell.detailTextLabel.text = @"Test";
cell.imageView.image = nil;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
Produces this..
In cellForRowAtIndexPath:
, you need to give the cell with the ad a different cell identifier when you dequeue old cells for reuse. Cells with different subview contents need to have different identifiers, otherwise it will simply pop off the cell that contains the adView whenever it is dequeuing cells from the stack for the Cell
identifier.
Basically, when the ad cell moves offscreen, it is put in the reuse queue and is subsequently popped off and reused for a normal cell when the normal cell at the opposite end of the table view comes into view from offscreen.
When indexPath.row == 0
(for each section), you'll need to dequeue the ad cell instead of a normal cell, like so:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellId = "Cell";
if(indexPath.row == 0) //first cell in each section
cellId = @"ad";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.row == 0)
[cell addSubview:bannerView_];
}
if (indexPath.row != 0){
cell.textLabel.text = @"Test";
cell.detailTextLabel.text = @"Test";
cell.imageView.image = nil;
}
return cell;
}
Also, check out an open-source library I wrote to manage both iAds and (fallback) AdMob ads with a single line of code. I haven't tested it with this particular config, but it may help nonetheless.
精彩评论