UitableView单元格之间的广告,该单元格使用核心数据与nsfetchedresultscontroller

发布于 2025-01-23 15:10:13 字数 1039 浏览 1 评论 0原文

(我已经推荐此stackoverflow Post ,但是我找不到我正在寻找的东西对于)

我需要在uitableView的单元格之间显示广告。

上图仅用于表示目的,并且不显示我正在使用的应用程序使用的实际设计/数据。

uitaiteView 来自核心数据通过nsfetchedResultScontroller使用fetchController.Object.object(at:indexpath),而AD单元格完全来自不同的数据源。

  • 由于需要在实际的表视图单元格之间显示广告,因此它将indexpath nsfetchedResultScontroller弄乱,以从core core data 提取请求。
  • 此外,用户可以选择从uiateview中删除任何实际单元格(非AD单元格),该可以在转移中更改单元格的indexpath
  • 单元格中可能显示的广告数量可变,现在假定它是常数c

请让我知道是否有一种有效处理所有这些边缘情况的方法,最好不要更改indexpath fetchcontroller.object中使用的对象(at:indexpath)

(I have already referred this StackOverflow post, however did not find what I was looking for)

I have a requirement to show ads in between cells of a UITableView.

enter image description here

The image above is used only for representational purposes, and does not show the actual design/data used by the app I am working with.

The normal cells of the UITableView come from a Core Data fetch request via NSFetchedResultsController using fetchController.object(at: indexPath), whereas the ad cells come from a different data source altogether.

  • Since the ads need to be shown in between the actual table view cells, it messes up the indexPath used by NSFetchedResultsController to retrieve the object from Core Data fetch request.
  • Also the user has the option to delete any of the actual cells (non-ad cells) from the UITableView, which can in-turn change the indexPath of the cells.
  • There can be variable number of ads shown in the cells, for now assume it to be a constant c.

Please let me know if there is a way to handle all these edge cases efficiently, preferably without changing the indexPath object used in fetchController.object(at: indexPath).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青芜 2025-01-30 15:10:13

您可以在表查看单元格类中添加标志,说明是否显示AD:

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
}

You can add a flag to your table view cell class, telling if to show an ad or not:

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
        }
    }
}

With the above in place, what's left is to store an array of randomly generated indices where you want to show the ads, and update the cell accordingly:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = ...
    cell.showAd = adIndices.contains(indexPath.row)
    return cell
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文