在故事板中创建了带有自定义单元格的UITableView。
单元格中的标签连接到插座:
@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
}