使用UITableViewCell制作MoPub本地广告有问题

时间:2021-02-18 07:20:19

I integrated MoPub advertising for my iOS app to run some native ads in my UITableViewController (actually, it's a PFQueryTableViewController because I'm using Parse to load data into the tableView).

我集成了MoPub的iOS应用程序广告,在UITableViewController中运行一些本地广告(实际上,它是一个PFQueryTableViewController,因为我使用Parse将数据加载到tableView中)。

Everything is running fine - the console shows that ads are being loaded successfully, and I can see the extra cell in my tableView where the ad should be - the problem is that none of the content for the ad is showing up; I only see a blank cell.

一切都运行得很好——控制台显示广告被成功加载,我可以在我的tableView中看到广告应该在的位置——问题是广告的内容没有显示出来;我只看到一个空白的单元格。

The problem doesn't seem to be from MoPub. I think I'm setting up my cell incorrectly because I can't get ANYTHING to show up there.

问题似乎不是来自MoPub。我想我设置的单元格不正确,因为我不能在那里显示任何东西。

I have tried 2 things.

我试过两件事。

  1. Setting up the cell using storyboard. I added a new cell to my UITableViewController and created a new class for it, NativeAdCell. I designed IBOutlets and connected them to the class file. The content is loaded through a MoPub function like so:

    使用故事板设置单元格。我向UITableViewController中添加了一个新单元格,并为它创建了一个新类,NativeAdCell。我设计了iboutlet并将它们连接到类文件。内容通过MoPub函数如下:

    func layoutAdAssets(adObject: MPNativeAd!) {
    adObject.loadIconIntoImageView(iconImageView)
    adObject.loadTitleIntoLabel(titleLabel)
    adObject.loadCallToActionTextIntoButton(callToActionButton)
    adObject.loadImageIntoImageView(mainImageView)
    adObject.loadTextIntoLabel(mainTextLabel)
    }
    

The app crashes at the first line with console output (lldb).

该应用程序在第一行与控制台输出(lldb)崩溃。

  1. Adding the outlets programmatically. In the NativeAdCell file I add outlets like so:

    以编程方式添加网点。在NativeAdCell文件中,我添加了这样的outlet:

    var mainTextLabel = UILabel()
    var titleLabel = UILabel()
    var iconImageView = UIImageView()
    var mainImageView = UIImageView()
    var callToActionButton = UIButton()
    

And later setting their frames. The content is loaded with the same MoPub function. Now there is no crash, but the ad cell shows up completely blank with no content. Even if I manually set mainTextLabel.text = "Please show up" nothing will show up.

然后设置它们的帧。内容包含相同的MoPub函数。现在没有崩溃,但广告单元格显示完全空白,没有内容。即使我手动设置mainTextLabel。文字= "请出现",什么也不会出现。

The crash when I try to load the IBOutlets makes me think something is wrong with the way I'm linking up the NativeAdCell to the tableView, but it seems like the MoPub SDK should handle this.

当我试图加载iboutlet时发生的崩溃使我认为将NativeAdCell连接到tableView的方式有问题,但似乎MoPub SDK应该处理这个问题。

Can anybody spot the problem based on this info?

有谁能根据这个信息发现问题吗?

2 个解决方案

#1


1  

I achieved this without IBOutlets as follows, it's not well documented so I figured this might help someone out who is looking to use the MoPub SDK with custom cells for native ads

我在没有iboutlet的情况下实现了这一点,如下所示,它没有得到很好的文档说明,因此我认为这可能会帮助那些希望使用MoPub SDK和自定义单元格进行本地广告的人

//
//  AdTableViewCell.swift
//  Trending-News
//
//  Created by Conor Griffin on 22/07/2015.
//  Copyright (c) 2015 Conor Griffin. All rights reserved.
//

import UIKit
import MoPub

class AdTableViewCell: UITableViewCell, MPNativeAdRendering {

    var iconImageView: UIImageView! = UIImageView()
    var titleLabel: UILabel! = UILabel()
    var mainTextLabel: UILabel! = UILabel()
    var advertIndicator: CGSponsoredLinkLabel! = CGSponsoredLinkLabel()
    var adCorner: CGCornerHighlight! = CGCornerHighlight()

    static func sizeWithMaximumWidth(maximumWidth: CGFloat) -> CGSize {
        return CGSize(width: maximumWidth, height: 100)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let width = CGRectGetWidth(frame)
        let cellFrame = CGRect(x: 0, y: 0, width: width, height: 100)

        iconImageView.layer.borderColor = UIColor.grayColor().CGColor
        iconImageView.layer.borderWidth = 1
        iconImageView.frame = CGRectMake(10, 10, 80, 80)
        contentView.addSubview(iconImageView)

        titleLabel.frame = CGRectMake(100, 10, self.frame.width - 110, 25)
        contentView.addSubview(titleLabel)

        mainTextLabel.frame = CGRectMake(100, 35, self.frame.width - 110, self.frame.height - 65)
        contentView.addSubview(mainTextLabel)
        mainTextLabel.numberOfLines = 3
        mainTextLabel.font = UIFont.systemFontOfSize(11.0)

        advertIndicator.frame = CGRectMake(100, self.frame.height - 30, self.frame.width - 110, self.frame.height - 80)
        contentView.addSubview(advertIndicator)
        advertIndicator.font = UIFont.systemFontOfSize(10.0)
        advertIndicator.text = "Advertisement"
        advertIndicator.textColor = UIColor.darkGrayColor()
        advertIndicator.backgroundColor = UIColor(red: 252/255.0, green: 246/255.0, blue: 220/255.0, alpha: 1.0)
        advertIndicator.clipsToBounds = true
        advertIndicator.layer.masksToBounds = true
        advertIndicator.layer.cornerRadius = 3

    }

    func layoutAdAssets(adObject: MPNativeAd!) {
        adObject.loadTitleIntoLabel(titleLabel)
        adObject.loadTextIntoLabel(mainTextLabel)
        adObject.loadIconIntoImageView(iconImageView)
    }

}

#2


0  

It's quite magical, really, that I forgot to add the subviews to the contentView of the NativeAdCell. self.contentView.addSubview(mainTextLabel) etc solves #2.

非常神奇的是,我忘记将子视图添加到NativeAdCell的contentView中。self.contentView.addSubview(mainTextLabel)等解决了# 2。

Not sure how to get this to work with IBOutlets still. It would be a lot easier that way (removes need for programmatic constraints).

还不知道如何让iboutlet应用这个功能。那样会容易得多(消除了编程约束的需要)。

#1


1  

I achieved this without IBOutlets as follows, it's not well documented so I figured this might help someone out who is looking to use the MoPub SDK with custom cells for native ads

我在没有iboutlet的情况下实现了这一点,如下所示,它没有得到很好的文档说明,因此我认为这可能会帮助那些希望使用MoPub SDK和自定义单元格进行本地广告的人

//
//  AdTableViewCell.swift
//  Trending-News
//
//  Created by Conor Griffin on 22/07/2015.
//  Copyright (c) 2015 Conor Griffin. All rights reserved.
//

import UIKit
import MoPub

class AdTableViewCell: UITableViewCell, MPNativeAdRendering {

    var iconImageView: UIImageView! = UIImageView()
    var titleLabel: UILabel! = UILabel()
    var mainTextLabel: UILabel! = UILabel()
    var advertIndicator: CGSponsoredLinkLabel! = CGSponsoredLinkLabel()
    var adCorner: CGCornerHighlight! = CGCornerHighlight()

    static func sizeWithMaximumWidth(maximumWidth: CGFloat) -> CGSize {
        return CGSize(width: maximumWidth, height: 100)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let width = CGRectGetWidth(frame)
        let cellFrame = CGRect(x: 0, y: 0, width: width, height: 100)

        iconImageView.layer.borderColor = UIColor.grayColor().CGColor
        iconImageView.layer.borderWidth = 1
        iconImageView.frame = CGRectMake(10, 10, 80, 80)
        contentView.addSubview(iconImageView)

        titleLabel.frame = CGRectMake(100, 10, self.frame.width - 110, 25)
        contentView.addSubview(titleLabel)

        mainTextLabel.frame = CGRectMake(100, 35, self.frame.width - 110, self.frame.height - 65)
        contentView.addSubview(mainTextLabel)
        mainTextLabel.numberOfLines = 3
        mainTextLabel.font = UIFont.systemFontOfSize(11.0)

        advertIndicator.frame = CGRectMake(100, self.frame.height - 30, self.frame.width - 110, self.frame.height - 80)
        contentView.addSubview(advertIndicator)
        advertIndicator.font = UIFont.systemFontOfSize(10.0)
        advertIndicator.text = "Advertisement"
        advertIndicator.textColor = UIColor.darkGrayColor()
        advertIndicator.backgroundColor = UIColor(red: 252/255.0, green: 246/255.0, blue: 220/255.0, alpha: 1.0)
        advertIndicator.clipsToBounds = true
        advertIndicator.layer.masksToBounds = true
        advertIndicator.layer.cornerRadius = 3

    }

    func layoutAdAssets(adObject: MPNativeAd!) {
        adObject.loadTitleIntoLabel(titleLabel)
        adObject.loadTextIntoLabel(mainTextLabel)
        adObject.loadIconIntoImageView(iconImageView)
    }

}

#2


0  

It's quite magical, really, that I forgot to add the subviews to the contentView of the NativeAdCell. self.contentView.addSubview(mainTextLabel) etc solves #2.

非常神奇的是,我忘记将子视图添加到NativeAdCell的contentView中。self.contentView.addSubview(mainTextLabel)等解决了# 2。

Not sure how to get this to work with IBOutlets still. It would be a lot easier that way (removes need for programmatic constraints).

还不知道如何让iboutlet应用这个功能。那样会容易得多(消除了编程约束的需要)。