0

我正在尝试使用 Facebook SDK (FBAudienceNetwork.framework) 添加原生广告。

我在 Facebook SDK 本身中找到了一个不错的教程。但我的问题是在 Tableview 中插入广告时,索引路径会增加。在didSelectRowAtIndexpath:tableview中选择单元格时,我得到了一个增加的 indexpath(错误的)。

我应该怎么做才能获得单元格的确切索引路径?

在此处输入图像描述

输出:索引路径 = 0,2

在此处输入图像描述

输出:索引路径 = 0,3

任何相关的帮助和建议表示赞赏。干杯!

4

2 回答 2

0

集成 FBNativeads 的正确方法是将FBNativeAdTableViewCellProvider.h类与[self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];

执行

1) .h 中的声明

    @interface TableviewViewController : UITableViewController<FBNativeAdsManagerDelegate>{

 int adRowStep;
}

    @property (strong, nonatomic) FBNativeAd *nativeAd;
    @property (strong, nonatomic) FBNativeAdsManager*adsManager;
    @property (retain, nonatomic) FBNativeAdTableViewCellProvider*adsCellProvider;

2) forNumAdsRequested下的ViewDidLoad(你可以自定义你想要多少广告)

-(void)viewDidLoad{

self.adsManager = [[FBNativeAdsManager alloc]initWithPlacementID:@"PLACEMENT_ID" forNumAdsRequested:3];  

self.adsManager.delegate = self;
[self.adsManager loadAds];

}

3) 从 FBNativeAdManager 加载的原生广告

-(void)nativeAdsLoaded{
    self.adsCellProvider = [[FBNativeAdTableViewCellProvider alloc]initWithManager:self.adsManager forType:FBNativeAdViewTypeGenericHeight120];

    self.adsCellProvider.delegate = self;

    if (self.tableView != nil) {
        [self.tableView reloadData];
    }
}

- (void)nativeAd:(FBNativeAd *)nativeAd didFailWithError:(NSError *)error
{
    NSLog(@"Native ad failed to load with error: %@", error);
}

- (void)nativeAdDidClick:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad was clicked.");
}

- (void)nativeAdDidFinishHandlingClick:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad did finish click handling.");
}

- (void)nativeAdWillLogImpression:(FBNativeAd *)nativeAd
{
    NSLog(@"Native ad impression is being captured.");
}

4) UITableview 部分 + 行设置

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


        if (self.adsCellProvider != nil) {

            return [self.adsCellProvider adjustCount:self.objects.count forStride:adRowStep];
        }

        else{

            return self.objects.count;
        }


    return 0;
}

5)按顺序返回不同的单元格

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

        CustomTableViewCell*cell;

     if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {

                cell = [self.tableView dequeueReusableCellWithIdentifier:@"facebook_cell" forIndexPath:indexPath];


                FBNativeAd*nativeAdx = self.adsManager.nextNativeAd;


                if (nativeAdx) {
                    [nativeAdx unregisterView];
                }


                NSURL*image_url =  nativeAdx.coverImage.url;
                NSURL*image_icon_url = nativeAdx.icon.url;


                dispatch_async(dispatch_get_global_queue(0,0), ^{
                    NSData * data = [[NSData alloc] initWithContentsOfURL: image_url];
                    NSData* data2 = [[NSData alloc] initWithContentsOfURL:image_icon_url];

                    if ( data == nil && data2 == nil)
                        return;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        // WARNING: is the cell still using the same data by this point??
                        cell.adCoverMediaView.image = [UIImage imageWithData:data];

                        cell.adIconImageView.image = [UIImage imageWithData:data2];

                    });

                });



                // Render native ads onto UIView
                cell.adTitleLabel.text = nativeAdx.title;
                cell.adBodyLabel.text = nativeAdx.body;
                cell.adSocialContextLabel.text = nativeAdx.socialContext;
                cell.sponsoredLabel.text = @"Sponsored";

                [cell.adCallToActionButton setTitle:nativeAdx.callToAction
                                           forState:UIControlStateNormal];



                cell.adChoicesView.nativeAd = nativeAdx;
                cell.adChoicesView.corner = UIRectCornerTopRight;


                [nativeAdx registerViewForInteraction:cell withViewController:self];


                return cell;
            }
            else{


                long dif = indexPath.row - (indexPath.row / adRowStep);

                 NSDictionary*object = [self.objects objectAtIndex:dif];

                 //Do whatever you want with your object + create his own cell
// This is not a Facebook Ad Cell


            }

    }

6) DidSelectRowAtIndexPath --> 用这个模式回答你的问题

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (self.adsCellProvider != nil && [self.adsCellProvider isAdCellAtIndexPath:indexPath forStride:adRowStep]) {



        }
        else{


            long dif = indexPath.row - (indexPath.row / adRowStep);

            PFObject*sender_obx = [self.objects objectAtIndex:dif];

            PostDetailsViewController*rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"post_detail_VC"];
            rootViewController.post_obx = sender_obx;
            rootViewController.post_id  = sender_obx.objectId;

            [self.navigationController pushViewController:rootViewController animated:YES];

        }

}

我就是这样做的。我希望它会帮助你

于 2018-04-03T04:44:58.637 回答
-1

实际上我会继承这个UITableViewCell类并给它添加一个模型属性。在 TableView 的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SubCellClass *cell = [tableView dequeueReusableCellWithIdentifier:cellName forIndexPath:indexPath];
    cell.model = [self.data objectAtIndex:indexPath.row];
    ...
}

方法,我将设置模型属性。

当我选择单元格时,我可以调用 cell.model 并直接获取数据。

于 2016-08-28T07:23:36.950 回答