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)