0

I've been attempting to embed google adverts into a uitable view ever "X" cells, i've followed this tutorial: (http://googleadsdeveloper.blogspot.com/2012/03/embedding-google-admob-ads-within.html), but i'm finding that the table view is not always showing all the data in the datasource when i add cells for the adverts, it usually off by a few cells. I believe this is to do with the numberOfRowsInInSection function, but for the life of me can't get my head around the math to calculate this correclty. So far i have the following: (kAdCellFrequency is currently 10 but this should be adjustable to an arbitrary value):

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    id<NSFetchedResultsSectionInfo> sectionInfo = [[eventResults sections] objectAtIndex:0];
    int sectionObjectsCount = [sectionInfo numberOfObjects];
    int advertCellCount = ([sectionInfo numberOfObjects])/kAdCellFrequency;
    int additionalCellCount = sectionObjectsCount%kAdCellFrequency;
    NSLog(@"event count %i Ad cell count %i additional count %i total cell count %i cells",sectionObjectsCount, advertCellCount, additionalCellCount, sectionObjectsCount+advertCellCount+additionalCellCount);
    SDMEvent *theEvent = [[eventResults fetchedObjects]objectAtIndex:sectionObjectsCount-1];
    NSLog(@"Final object name %@",theEvent.eventName);
    return sectionObjectsCount+advertCellCount+additionalCellCount;
    //return sectionObjectsCount;
}

I added the additionalCellCount variable as when using the value 10 for kAdCellFrequency the data was 3 cells away from correct, but this same math fails to display the correct amount if kAdCellFrequency is set to 5.

The creation of the cells advert cell or standard event cell:

 adCell = (UICellWithAdvert *) [tableView dequeueReusableCellWithIdentifier:@"UICellWithAdvert"];
        eventCell = (UISDMEventCell *) [tableView dequeueReusableCellWithIdentifier:@"UISDMEventCell"];

        NSLog(@"index: %i cell freq: %i result: %i",[indexPath row], kAdCellFrequency, [indexPath row]%kAdCellFrequency);
        if (([indexPath row] % kAdCellFrequency) == kAdCellFrequency-1) { // Advert cell dont add cell at index 0!

            if (!adCell) {
                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"UICellWithAdvert" owner:nil options:nil];

                for(id currentObject in topLevelObjects){
                    if ([currentObject isKindOfClass:[UICellWithAdvert class]]) {
                        adCell = (UICellWithAdvert *)currentObject;
                    }
                }
            }

            // remove the advert from its current cell
            [googleAdvert removeFromSuperview];

            // add to cell if it doesn't have advert
            if (!googleAdvert.superview) {

                [adCell addSubview:googleAdvert];
                NSLog(@"Add cell with Advert");
            }

            return adCell;
        }
        else{ // event cell

            if (!eventCell) {
                NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"UISDMEventCell" owner:nil options:nil];

                for(id currentObject in topLevelObjects){
                    if ([currentObject isKindOfClass:[UISDMEventCell class]]) {
                        eventCell = (UISDMEventCell *)currentObject;
                    }
                }
            }

            return eventCell;
        }

The displaying of the content at the cell index, i've taken out the actual setting of data, just need the calculation:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    int calculatedIndex = [indexPath row]-floor((float)[indexPath row]/kAdCellFrequency);
    NSLog(@"Calculated %i",calculatedIndex);
    NSLog(@"Normal Index %i",[indexPath row]);

}

Any help would be great, even a pointer to somewhere where i can learn more about this math?

Update: OK it appears that the cell count is returning correctly, but on some occasions the code is setting duplicating the content for some cells so for example cell 320 + 321 display the same event. I think this is related to the following math in my original code (this is pseudo code):

adCellFrequency = 10

cellIndex = 319 
int calculatedIndex = [indexPath row]-floor((float)[indexPath row]/kAdCellFrequency); // equals 288

cellIndex = 320 
int calculatedIndex = [indexPath row]-floor((float)[indexPath row]/kAdCellFrequency); // equals 288

Notice the values for both cells equal 288 meaning that two cells now display the same content. So the number of cells is correct but the content rendered for the index is incorrect. (PaulW - this issue also occurs when i converted your code, are you getting this.. i'm maxing out at 325 cells)

SmokersCough
  • 967
  • 7
  • 22
  • It was an interesting problem, so I took a look. I don't want to provide an answer that is just a code dump, so here is a gist that is a code dump :) https://gist.github.com/paulw11/3dc18c3844df100863033f41890e532c – Paulw11 Jun 14 '16 at 23:28
  • Sorry, I just realised that you are using Objective C and I wrote my solution in Swift. Hopefully you can review the last three functions; they perform the maths and should be pretty simple to convert to Objective C – Paulw11 Jun 14 '16 at 23:30
  • Hi Paul, thanks for such a detailed answer, i converted your code to objective c but i'm getting the same issue, however it appears that your code has helped me narrow down the issue. It appears that the math tends to return the same value for more than one cell, hence the cell count is correct but the items being set for the cell are incorrect, i believe this is exactly what was happening with my code as well. I've added an example of what i'm talking about in the main part of my question, any thoughts? – SmokersCough Jun 15 '16 at 11:36
  • Hmm. My test just displayed row numbers and it didn't skip any. – Paulw11 Jun 15 '16 at 11:42
  • I just added an objective C file to the gist that has the function you should use in place of the code you have for getting `calculatedIndex` – Paulw11 Jun 15 '16 at 11:55
  • And I tested my code with 325 rows, ad interval=10 – Paulw11 Jun 15 '16 at 12:02
  • Thank you so much Paul for your help!! It was my mistake i failed to modify my willDisplayCode to render the correct object.. Your code was indeed correct. I think i've been looking at the problem for too long. Once again thank you for taking the time to give me such constructive feedback and a complete answer to the problem. If you'd like to add the answer as another Post i'll mark it as correct. – SmokersCough Jun 15 '16 at 12:25
  • @SmokersCough : Is it possible to show different adds on each recurring items? Please post your answer here. https://stackoverflow.com/q/62485567/6285383 – Priya Jun 22 '20 at 01:27

1 Answers1

0

Teaching you how to fish...

  1. Look at how your rounding is actually being managed (look at lrintf() for example and run some sample outputs of a range of numbers using a for/loop.
  2. Why not output the calculatedIndex value to a label on the cell so that you can visualise the problem more clearly?
  3. Look at quick ways to visualise the maths - even a spreadsheet can help.
Dominic
  • 258
  • 2
  • 8
  • Hi dominic, i've actually approached this in a similar way to what you have described, drawing out the cells and labeling the indexes and then the assumed indexes. It's not actually the indexes being calculated but rather the cell count being returned that appears to be the issue. That's where the issue appears to lie, and its a little difficult to visualise something based on an arbitrary value. – SmokersCough Jun 14 '16 at 13:30
  • So you have done a spreadsheet of values such as, index = 9, cell = 9; index = 10, cell = advert 1; index = 11, cell = 10 - where you can calculate the cell from the index? – Dominic Jun 14 '16 at 13:35
  • Not a spreadsheet but the math itself is solid, i have a drawing of a table view with the formula and value of each index calculated based on the addition of the advert cell... however my content can go into the hundreds so i can't draw this out to that value, – SmokersCough Jun 14 '16 at 14:05