【问题标题】:Native Ad using Facebook in UITableView在 UITableView 中使用 Facebook 的原生广告
【发布时间】:2016-08-28 06:54:21
【问题描述】:

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

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

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

输出:索引路径 = 0,2

输出:Indexpath = 0,3

感谢任何相关的帮助和建议。干杯!

【问题讨论】:

    标签: ios objective-c facebook uitableview native-ads


    【解决方案1】:

    集成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];
    
            }
    
    }
    

    我就是这样做的。希望对你有帮助

    【讨论】:

    • 您使用的是哪个 SDK 版本?
    【解决方案2】:

    其实我会把UITableViewCell这个类子类化,并给它添加一个model属性。在TableView的

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

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

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

    【讨论】:

    • 感谢您的回复。但我的问题是要让单元格您需要正确选择的索引路径,对吗?在 tableview 中添加广告后,这里的 indexpath 正在发生变化。那就是问题所在。请参考图片。
    • 是否可以分别获取广告标题、图标、文本并在我们的设计中显示在表格视图单元格中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多