2015-07-12 84 views
0

我在我的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參數現在是異同步。

如何確保數據數組的索引和表的行保持同步?

+0

嘿康納爾, 如果使用[MPTableViewAdPlacer(https://github.com/mopub/mopub-ios-sdk/wiki/Native-Ads-Integration#place-ads-in-a-uitableview) 以及MoPub的iOS文檔'原生廣告集成'中定義的UITableView方法調用的相應mp替換,這將解決索引同步問題。 – Edward

+0

是的,我正在使用所有我認爲需要的 – conorgriffin

+0

最好的東西可能是製作一個小樣本項目來演示問題 – conorgriffin

回答

2

您可以在editActionsForRowAtIndexPath中的當前indexPath處獲取單元格。通過這個單元格,我們可以得到合適的indexPath,而不用包含mp_indexPathForCell的廣告索引。

由於editActionsForRowAtIndexPath接受IndexPath,當包含廣告時,它不同,我們必須有一種方法來編輯indexPath爲正確的參數。

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

上面的代碼將indexPath替換爲修改後的indexPath,但不包含廣告索引。