0

带有自定义单元格的 UITableView 是在 Storyboard 中创建的。

单元格中的标签连接到出口:

@IBOutlet weak var label: UILabel!

我在创建单元格时设置标签的文本。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    if (shouldPresentNativeAds)
    {
        return self.createAdCellForCollectionView(collectionView, cellForItemAtIndexPath: indexPath)
    }
    else
    {
        return self.createRegularCellForCollectionView(collectionView, cellForItemAtIndexPath: indexPath)
    }
}

问题仅发生在 adCells

func createAdCellForCollectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NSStringFromClass(FeedAdCollectionViewCell), forIndexPath: indexPath) as! FeedAdCollectionViewCell

    let nativeAdd = nativeAdsManager.nextNativeAd() as FBNativeAd?

    if (nativeAdd != nil)
    {
        cell.setDetails(nativeAdd!)
        return cell
    }
    else
    {
        return self.createRegularCellForCollectionView(collectionView, cellForItemAtIndexPath: indexPath)
    }
}

在单元格中:

func setDetails(nativeAd:FBNativeAd)
{
    if ((self.nativeAd) != nil) {
        self.nativeAd.unregisterView()
    }

    self.nativeAd = nativeAd

    if let nativeAdTitle = nativeAd.title
    {
        label.text = adTitleString
    }

    if let topController = UIApplication.topViewController() {
        nativeAd.registerViewForInteraction(self, withViewController: topController)
    }
}

出于某种原因,有时标签会重叠。

我在单元格代码中添加了以下内容:

override func prepareForReuse()
{
    super.prepareForReuse()
    print("prepareForReuse")
    self.label.text = ""
}

但这没有帮助。

日志是正确的:prepareForReuse 总是在设置标签文本之前被调用。但有时它是重叠的。可以做什么?

    if let nativeAdd = nativeAdsManager.nextNativeAd() as FBNativeAd?
    {
        cell.setDetails(nativeAdd!)
        return cell
    }

在此处输入图像描述

4

0 回答 0