是否可以将ios广告放入合适的单元格中?

时间:2015-06-25 05:02:57

标签: ios objective-c xcode

是否可以将ios广告放入合适的单元格?我相信它在xcode中是可行的,但苹果是否接受它?

1 个答案:

答案 0 :(得分:1)

您可以使用google admob在tableviewcell中显示添加内容。

有两种方法可以在表视图单元格中实现广告:

在整个表格中仅使用一个GADBannerView。这意味着相同的广告会显示在表格中的不同单元格中(首选)。 利用dequeueReusableCellWithIdentifier:这样只有在创建新单元格时才会创建GADBannerViews。 在整个表格中使用一个GADBannerView确实可以降低广告多样性,但通常可以提高展示广告的点击率,因为用户更有可能看到展示的广告。以下示例将说明如何实施此方法,因为这是更好的做法。

解决方案 - 单GADBannerView方法 出于本示例的目的,我们假设列表中的每个第10个元素都是广告。这意味着tableView:cellForRowAtIndexPath:将设置一个条件,修改每个第十个单元格的GADBannerView的位置:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  int row = [indexPath row];

  // Assume that kFrequencyAdsInCells is 10 so every 10th row is an ad
  // Don't want first item to be an ad so use 10-1=9 instead of 0
  if ((row % kFrequencyAdsInCells) == (kFrequencyAdsInCells)) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //Need to create a new cell object
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
                                       reuseIdentifier:CellIdentifier]
                autorelease];
    }
    // If bannerView_ has a superview, then it has been added to a cell
    if (!bannerView_.superview) {
      // First ad request made, make the ad request and add it to this cell
      [self requestAd];     
    }
    // The banner will be removed from the other cell and put into here
    [cell.contentView addSubview:bannerView_];


}
…

由于广告现在已插入到表视图中,因此之前映射到模型中数据的任何内容都将丢失。这里需要一些快速数学来弄清楚表格视图中的行如何排列。

 // Complete in cellForRowAtIndexPath: if not ad

  // Make sure we get all of the items from our model
  row = row - floor((float)row/kFrequencyAdsInCells);

  cell.textLabel.text = [dataController_ objectInListAtIndex:row];

您可以在此处找到更多详细信息Embedding Google AdMob Ads Within A UITableView