我一直在尝试将谷歌广告嵌入到一个合适的视图“X”单元格中,我遵循了这个教程:( http://googleadsdeveloper.blogspot.com/2012/03/embedding-google-admob-ads-within .html),但我发现当我为广告添加单元格时,表格视图并不总是显示数据源中的所有数据,它通常会关闭几个单元格。我相信这与numberOfRowsInInSection
函数有关,但对于我的一生来说,我无法通过数学来计算这个正确性。到目前为止,我有以下内容:(kAdCellFrequency 当前为 10,但这应该可以调整为任意值):
-(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;
}
我添加了 additionalCellCount 变量,因为当 kAdCellFrequency 使用值 10 时,数据距离正确的 3 个单元格,但如果 kAdCellFrequency 设置为 5,同样的数学运算无法显示正确的数量。
单元格广告单元格或标准事件单元格的创建:
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;
}
单元格索引处内容的显示,我取出了实际设置的数据,只需要计算:
-(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]);
}
任何帮助都会很棒,甚至是指向我可以了解更多关于这个数学的地方的指针?
更新:好的,单元格计数似乎正确返回,但在某些情况下,代码设置复制某些单元格的内容,例如单元格 320 + 321 显示相同的事件。我认为这与我原始代码中的以下数学有关(这是伪代码):
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
请注意,两个单元格的值都等于 288,这意味着两个单元格现在显示相同的内容。所以单元格的数量是正确的,但为索引呈现的内容是不正确的。(PaulW - 当我转换你的代码时也会出现这个问题,你得到这个.. 我在 325 个单元格中达到最大值)