UITableView和GADBannerView

时间:2014-01-30 16:13:09

标签: xamarin.ios admob uitableview

我试图在我的源代码行之间实现一个TableView,我想插入一个GADBannerView(由Google AdMob赞助的广告)。我目前有一个数据源,每当我到最后添加10个元素,如无限滚动。

我的意图是每6行插入一个元素GADBannerView。我该怎么办?

更新

如建议

public override int RowsInSection (UITableView tableview, int section)
{
    return (int)Math.Floor (Convert.ToDouble(ViewModel.ListElements.Count / 6) )+ ViewModel.ListElements.Count;
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
    if(indexPath.Row % 6 == 0 && indexPath.Row > 0){
        var cell = tableView.DequeueReusableCell (BannerGATableCell.Key) as BannerGATableCell ?? BannerGATableCell.Create ();

        _bannerView.LoadRequest (GADRequest.Request);
        cell.AddSubview(_bannerView);

        return cell;
    }

    if (indexPath.Row >= 6) {
        return base.GetCell (tableView,  NSIndexPath.FromRowSection((int)(indexPath.Row - (int)(indexPath.Row / 6)),0));
    }


    return base.GetCell (tableView, indexPath);
}

1 个答案:

答案 0 :(得分:0)

首先,您需要知道有多少横幅适合您的来源。

  • 假设您的来源共有14行。
  • 第6行应该是横幅。
  • 横幅数==(int)Math.Floor(14f / 6)== 2

所以我们在第6行(索引5)和第12行(索引11)有一个横幅。

RowsInSection()方法中,将 2 添加到您拥有的行数(横幅广告)。

然后在GetCell()中检查是否该注入横幅:

if(indexPath.Row % 6 == 0 && indexPath.Row > 0)
{
  var bannerCell = tableView.DequeueReusableCell(SOME_SPECIAL_CELL_ID) as BannerCell;
  if(bannerCell == null) 
  {
     ...
  }

  bannerCell.SetBannerView(SomeHelper.GetBannerView());
  return bannerCell;
}

BannerCell将是您必须创建的UITableViewCell的子类,允许设置横幅。您还可以使用普通单元格并将横幅视图添加到单元格的ContentView。但不要忘记先删除子视图。