我在我的iOS應用中使用MoPub來顯示UITableView
中的原生廣告。我遇到了一個問題,我得到fatal error: Array index out of range
錯誤,我明白爲什麼,但我無法弄清楚如何解決它。在iOS上使用editActionsForRowAtIndexPath和MoPub
MoPub將行插入到UITableView
中,因此參數到editActionsForRowAtIndexPath
包括包含廣告的行。但是,該表的數據陣列不包含這些行,因此對於插入的每個廣告,與該行的數據陣列中的索引相比,顯示數據的行的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廣告的行,得到以下輸出:
indexPath:0
indexPath :1
indexPath:3
indexPath:4
所以MoPub廣告在indexPath參數但是因爲該行不在我的數據源陣列的的tableView存在被計數,則indexPath參數現在是異同步。
如何確保數據數組的索引和表的行保持同步?
嘿康納爾, 如果使用[MPTableViewAdPlacer(https://github.com/mopub/mopub-ios-sdk/wiki/Native-Ads-Integration#place-ads-in-a-uitableview) 以及MoPub的iOS文檔'原生廣告集成'中定義的UITableView方法調用的相應mp替換,這將解決索引同步問題。 – Edward
是的,我正在使用所有我認爲需要的 – conorgriffin
最好的東西可能是製作一個小樣本項目來演示問題 – conorgriffin