【问题标题】:Using editActionsForRowAtIndexPath with MoPub on iOS在 iOS 上将 editActionsForRowAtIndexPath 与 MoPub 一起使用
【发布时间】:2015-07-12 12:13:59
【问题描述】:

我在我的 iOS 应用程序中使用 MoPub 在 UITableView 中显示原生广告。我遇到了一个问题,我收到了fatal error: Array index out of range 错误,我明白为什么,但我不知道如何解决它。

MoPub 在UITableView 中插入行,因此editActionsForRowAtIndexPathindexPath 参数包括包含广告的行。但是,表的数据数组不包括这些行,因此对于插入的每个广告,与该行的数据数组中的索引相比,显示数据的行的indexPath 为 +1。请参阅下面的editActionsForRowAtIndexPath 方法。

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    // fetch the data for the currently selected row
    let currentDictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>
    self.shareURL = currentDictionary["link"]!

    var shareAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Share" , handler: { 
        (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in

        NSLog("indexPath:%d", indexPath.row)

    })

    return [shareAction]
}

例如,如果我在第三行有一个广告(索引 = 2),在非 MoPub 广告行上滑动,我会得到以下输出:

索引路径:0
索引路径:1
索引路径:3
索引路径:4

因此,MoPub 广告被计入 indexPath 参数,但由于该行在我的 tableView 数据源数组中不存在,因此 indexPath 参数现在不同步。

如何确保数据数组的索引和表的行保持同步?

【问题讨论】:

  • 嘿 Conor,如果您使用 MPTableViewAdPlacer 以及 MoPub 的 iOS 文档“原生广告集成”中定义的 UITableView 方法调用的相应 mp 替换,这将解决您的索引同步问题。
  • 是的,我正在使用我认为我需要的所有这些
  • 最好的办法可能是制作一个小示例项目来演示问题
  • 嘿康纳,你能实现这样的东西吗? let indexPath = self.tableView.mp_indexPathForSelectedRow mp_indexPathForSelectedRow 返回所选行的原始索引路径,就好像没有插入任何广告一样。
  • 我试过了,但是当您在一行上向左滑动以触发 editActionsForRowAtIndexPath 方法时,这不会选择该行。因此,如果尚未选择任何行,则所选行将为 nil,如果已选择行,则所选行将为另一行。两者都不好

标签: ios swift uitableview twitter-fabric mopub


【解决方案1】:

您可以在editActionsForRowAtIndexPath 中获取当前indexPath 处的单元格。有了这个单元格,我们就可以得到适当的 indexPath,而无需使用 mp_indexPathForCell 包含广告索引。

由于editActionsForRowAtIndexPath接受了IndexPath,这在包含广告时是不一样的,我们必须有办法将indexPath编辑为正确的参数。

let cell = cellForRowAtIndexPath(indexPath)
let indexPath = mp_indexPathForCell(cell)

以上代码将 indexPath 替换为修改后的 indexPath,但不包含广告索引。

【讨论】:

    猜你喜欢
    • 2016-01-23
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 2014-01-10
    • 2017-11-06
    • 2016-10-23
    相关资源
    最近更新 更多