gpt4 book ai didi

ios - UITableView GADBannerView

转载 作者:行者123 更新时间:2023-12-01 18:32:20 27 4
gpt4 key购买 nike

我正在关注 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;
}

产生这个..

enter image description here

最佳答案

cellForRowAtIndexPath: ,当您将旧单元格出列以供重复使用时,您需要为带有广告的单元格提供不同的单元格标识符。具有不同 subview 内容的单元格需要具有不同的标识符,否则它会在从堆栈中为 Cell 出列单元格时简单地弹出包含 adView 的单元格。标识符。

基本上,当广告单元格移出屏幕时,它会被放入重用队列中,然后当表格 View 另一端的普通单元格从屏幕外进入 View 时,它会被弹出并重用于普通单元格。

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

另外,请查看 open-source library我写了一行代码来管理 iAd 和(后备)AdMob 广告。我没有用这个特定的配置测试它,但它可能会有所帮助。

关于ios - UITableView GADBannerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7368128/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com