Using editActionsForRowAtIndexPath with MoPub Native Ads


#1

I’m using the editActionsForRowAtIndexPath function to set which edit actions can be performed on a given table row, the content of this method is below. I’m hitting issues with the indexPath.row value being out of bounds for the tableview’s data array. I believe this is being caused by the mopub integration but I’m not sure how I can fix it.

When I slide left on a row, this method is called and the indexPath parameter is pointing to the row index that includes the mopub native ads above it. This causes a problem because the array of data for the table datasource does not include the native ads. So the indexPath being passed to the method is higher than the indexPath that the cell is displaying data from. How can I get the editActionsForRowAtIndexPath method to be called with the correct indexPath value that does not include the mopub ads? This will avoid the index out of bounds error when the line below is called:

    let currentDictionary = xmlParser.arrParsedData[indexPath.row] as Dictionary<String, String>

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

For example, if I have an advert in the third row (index = 2), swiping on the non-MoPub advert rows, I get the following output:

indexPath:0
indexPath:1
indexPath:3
indexPath:4

So the MoPub advert is being counted in the indexPath parameter but since that row doesn’t exist in my datasource array for the tableView, when I get closer to the end of the table I get fatal error: Array index out of range because the indexPath parameter is higher than the length of my array.


#2

See below


#3

Thanks, but the editActionsForRowAtIndexPath method doesn’t have a parameter ‘sender’


#4

Hey Conor,

Okay. I noticed that we can get the cell at the current indexPath within editActionsForRowAtIndexPath. With this cell, we can get the appropriate indexPath without including the ads indices with mp_indexPathForCell.

Since editActionsForRowAtIndexPath takes in IndexPath, which is not the same when ads are included, we have to have a way to edit indexPath to the correct parameters.

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

The above code replaces the indexPath with the modified indexPath without the ad indices included.


#5

Brilliant, thanks. That works, don’t know how I didn’t figure that one out. Thought I tried everything :smile:


#6

Hey Conor,

Thanks for your patience on this. Took me some time to realize it was that simple as well! If you need anything else, please reach out. I’ll crosspost to the same stackoverflow question.

Edward