簡體   English   中英

UITableView GADBannerView

[英]UITableView GADBannerView

我正在關注Google AdMob Ads iOS教程,以使廣告正常運行。 一切順利,直到我嘗試將廣告添加到UITableView。 我的設計是在表格上有兩個部分,其中廣告將出現在第一部分,而表格數據出現在第二部分。 然而,由於我在第一部分中獲得了廣告,但這並不能很好地工作,但它也是每10個單元重復一次。 我只想要廣告一次。 我該怎么做呢。

這是我的代碼......

- (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,
                                        0.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;
}

產生這個......

在此輸入圖像描述

cellForRowAtIndexPath: ,當您將舊單元格出列以便重復使用時,您需要為帶有廣告的單元格提供不同的單元格標識符。 具有不同子視圖內容的單元格需要具有不同的標識符,否則只要它從Cell標識符的堆棧中出列單元格,它就會彈出包含adView的Cell

基本上,當廣告單元在屏幕外移動時,它被放入重用隊列中,隨后當桌面視圖另一端的正常單元從屏幕外進入視圖時,它會被彈出並重新用於正常單元。

indexPath.row == 0 (對於每個部分)時,您需要將廣告單元格而不是普通單元格出列,如下所示:

// 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;
}

另外,查看我編寫的一個開源庫 ,用一行代碼管理iAd和(后備)AdMob廣告。 我沒有用這個特定的配置測試它,但它可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM