首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我在uitableview中使用广告,列表中的每4项都是广告

我在uitableview中使用广告,列表中的每4项都是广告
EN

Stack Overflow用户
提问于 2016-11-11 10:39:56
回答 3查看 123关注 0票数 1

我在UITableView中使用广告,其中列表中的每4项左右都是一条广告。它可以工作,但是广告的位置像1-2-3- ad -5-6-7-ad,而不是1-2-3-ad-4-5-6-ad。

我的UITableView方法

代码语言:javascript
运行
复制
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section==0)
    {
            return 0;
    }
    else{
        return [self.articlesArray count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSInteger row = [indexPath row];
    if (3 == (row % 4)) {  // or 0 == if you  want the first cell to be an ad!
        static NSString *MyIdentifier = @"AdCell";
        AdViewCell  *cell = (AdViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        if ((cell == nil) || (![cell isKindOfClass: AdViewCell.class] {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AdCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
            cell = [[AdViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                               reuseIdentifier:MyIdentifier] ;
        }
        GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeMediumRectangle];

        bannerView =[[GADBannerView alloc] initWithFrame:CGRectMake(width,heigth,300,250)];

        bannerView.adUnitID =@"";
        bannerView.rootViewController =self;
        GADRequest *request = [GADRequest request];
        [bannerView loadRequest:request];

        [cell.contentView addSubview:bannerView];

        return cell;
    }
    else {
        static NSString *simpleTableIdentifier = @"ArticleCell";
        ArticleViewCell *cell = (ArticleViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ((cell == nil) || (![cell isKindOfClass: ArticleViewCell.class]))
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticleCell" owner:self options:nil];
            cell = [nib objectAtIndex:1];
            cell = [[ArticleViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:simpleTableIdentifier] ;
        }

        NSInteger offset = indexPath.row / 4;
        NSInteger roww = indexPath.row - offset;
        NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:roww];

        NSString *imageUrl = [[self.articlesArray objectAtIndex:indexPath.row]objectForKey:@"featured_image_thumbnail_tab_url"];
        imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl ] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            // Set your image over here
        }else{
            //something went wrong
            NSLog(@"Error occured : %@", [error description]);
        }
   }];

    NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"];

    cell.titleLabel.text = title;

    NSString * excerpt=[tempDictionary valueForKeyPath:@"excerpt.rendered"];
    NSString *excerpt_rend =[self convertHTML:excerpt];
    cell.excerptLabel.text = excerpt_rend;

    return cell;
    }
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   if ([[segue identifier] isEqualToString:@"showarticle"]){
   NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
   DGArticleViewController *articleViewController =    (DGArticleViewController *)segue.destinationViewController;
   articleViewController.articlesDetails = [self.articlesArray objectAtIndex:rowww];  //rowww = roww
   NSLog(@"data article %@",articleViewController.articlesDetails);
}
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-11 11:01:25

引入显示广告数量的偏移量:

代码语言:javascript
运行
复制
NSInteger offset = indexPath.row / 4
NSIngteger adjustedRow = indexPath.row - offset
// use row from adjustedRow, instead of indexPath.row
NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:adjustedRow];
...
NSString *imageUrl = [[self.articlesArray objectAtIndex:adjustedRow]objectForKey:@"featured_image_thumbnail_tab_url"];

另外,还需要调整numberOfRowsInSection:,否则您缺少的项目如下:

代码语言:javascript
运行
复制
return [self.articlesArray count] + [self.articlesArray count] / 4;

prepareForSegue

代码语言:javascript
运行
复制
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
NSInteger *adjusted = indexPath.row - indexPath.row / 4
...
articleViewController.articlesDetails = [self.articlesArray objectAtIndex:adjusted];
...

测试:

代码语言:javascript
运行
复制
for (int testIndexPathRow = 0; testIndexPathRow < 100; ++testIndexPathRow) {
    if (3 == testIndexPathRow % 4) {
        NSLog(@"Ad");
    } else {
        NSInteger offset = testIndexPathRow / 4;
        NSInteger row = testIndexPathRow - offset;
        NSLog(@"data at index: %d", row);
    }
}

指纹:

代码语言:javascript
运行
复制
2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 0
2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 1
2016-11-11 12:09:16.927 TEST[7126:382810] data at index: 2
2016-11-11 12:09:16.928 TEST[7126:382810] Ad
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 3
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 4
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 5
2016-11-11 12:09:16.928 TEST[7126:382810] Ad
2016-11-11 12:09:16.928 TEST[7126:382810] data at index: 6
...
票数 1
EN

Stack Overflow用户

发布于 2016-11-11 10:59:15

您必须调整indexpath.row以反映跳过的单元格。因此,在您的其他部分中,将indexPath.row替换为adjustedRow编号。

代码语言:javascript
运行
复制
else
{

static NSString *simpleTableIdentifier = @"ArticleCell";
ArticleViewCell *cell = (ArticleViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ((cell == nil) || (![cell isKindOfClass: ArticleViewCell.class]))
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ArticleCell" owner:self options:nil];
cell = [nib objectAtIndex:1];
cell = [[ArticleViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:simpleTableIdentifier] ;
}

NSInteger adjustedRow = indexPath.row - indexPath.row/4;
NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:adjustedRow];

 NSString *imageUrl = [[self.articlesArray objectAtIndex:adjustedRow]objectForKey:@"featured_image_thumbnail_tab_url"];
 imageUrl = [imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 [cell.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl ] placeholderImage:nil options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image){
    // Set your image over here
}else{
    //something went wrong
    NSLog(@"Error occured : %@", [error description]);
}
}];

NSString * title=[tempDictionary valueForKeyPath:@"title.rendered"];

cell.titleLabel.text = title;


NSString * excerpt=[tempDictionary valueForKeyPath:@"excerpt.rendered"];
NSString *excerpt_rend =[self convertHTML:excerpt];
cell.excerptLabel.text = excerpt_rend;

return cell;
}
票数 0
EN

Stack Overflow用户

发布于 2016-11-11 11:09:27

例如,您有8行的数组,还需要2个ads,这意味着总共有10行。但是,您只传递会造成问题的数组计数。要解决这个问题,请遵循下面的代码

代码语言:javascript
运行
复制
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if (section==0)
    {
       return 0;
    }
    else
    {

       return [self.articlesArray count] + ([self.articlesArray count]/4);
    }
}

从数组中获取数据,如下所示,它肯定会帮助您

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
       //Your current code
       int index = indexPath.row/4
       NSDictionary * tempDictionary = [self.articlesArray objectAtIndex:indexPath.row-index];

       //Your current code
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40546220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档