2011-09-09 4 views
1

私はGoogle AdMob Ads iOSのチュートリアルに従っています。 すべて私はUITableViewに広告を追加しようとするまでうまく動作します。 私のデザインは、最初のセクションに広告が表示される2つのセクションと、2番目のセクションの表データを持つというものでした。しかし、これはあまりうまくいきませんが、最初のセクションで広告が表示されますが、10番目のセルごとに繰り返されます。私は一度だけ広告をしたい。どのように私はこれを行うのですか?ここでUITableView GADBannerView

は私のコードです...

- (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:でこの..

enter image description here

+0

tableviewcellに追加する前に広告をリクエストしています。これは、ユーザーが画面に表示する前に広告を作成したためにインプレッションが送信されます。 –

答えて

4

を作成し、あなたがデキューするとき、あなたは広告異なるセル識別子でセルを与える必要が古い細胞を再利用する。サブビューの内容が異なるセルには異なる識別子が必要です。そうでない場合は、Cell識別子のスタックからセルをデキューするたびにadViewを含むセルがポップされます。

基本的には、広告セルが画面外に移動すると、それは再利用キューに入れられ、テーブルビューの反対側にある通常のセルがオフスクリーンから見えるときにポップオフされ、通常のセルに対して再利用されます。 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を管理するために書いたopen-source libraryをチェックアウトし、 1行のコードでAdMob広告をフォールバックします(代替)。私はこの特定の設定でそれをテストしていませんが、それでも役に立つかもしれません。