(我已經提到
上圖僅用于代表性目的,并未顯示我正在使用的應用程式使用的實際設計/資料。
普通單元格UITableView來自通過using的Core Data獲取請求,而廣告單元格則完全來自不同的資料源。NSFetchedResultsControllerfetchController.object(at: indexPath)
- 由于廣告需要顯示在實際的表格視圖單元格之間,因此它弄亂了
indexPath用于NSFetchedResultsController從Core Data獲取請求中檢索物件的方法。 - 此外,用戶可以選擇從 中洗掉任何實際單元格(非廣告單元格)
UITableView,這反過來又可以更改indexPath單元格的 。 - 單元格中可以顯示可變數量的廣告,現在假設它是一個常數
c。
請讓我知道是否有一種方法可以有效地處理所有這些邊緣情況,最好不要indexPath更改fetchController.object(at: indexPath).
uj5u.com熱心網友回復:
您可以在表格視圖單元格類中添加一個標志,告知是否顯示廣告:
class MyTableViewCell: UITableViewCell {
var showAd: Bool = false {
didSet {
// `adView` here being either an IBOutlet from the XIB,
// or a programatically created view, depending on how
// you designed the cell
adView.isHidden = !showAd
}
}
}
完成上述操作后,剩下的就是在要顯示廣告的位置存盤一組隨機生成的索引,并相應地更新單元格:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ...
cell.showAd = adIndices.contains(indexPath.row)
return cell
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/464349.html
標籤:迅速 合适的视图 核心数据 nsfetchedresults 控制器
上一篇:表視圖設計重疊
