【发布时间】:2016-06-14 11:47:30
【问题描述】:
我一直在尝试将谷歌广告嵌入到“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 个单元格中达到最大值)
【问题讨论】:
-
这是一个有趣的问题,所以我看了一下。我不想提供只是代码转储的答案,所以这里有一个代码转储的要点:) gist.github.com/paulw11/3dc18c3844df100863033f41890e532c
-
抱歉,我刚刚意识到您使用的是 Objective C,我用 Swift 编写了我的解决方案。希望您可以查看最后三个功能;他们执行数学运算,应该很容易转换为 Objective C
-
嗨,保罗,感谢您提供如此详细的答案,我将您的代码转换为目标 c,但我遇到了同样的问题,但您的代码似乎帮助我缩小了问题的范围。似乎数学倾向于为多个单元格返回相同的值,因此单元格计数是正确的,但为单元格设置的项目不正确,我相信这正是我的代码所发生的事情。我在问题的主要部分添加了一个示例,说明我在说什么,有什么想法吗?
-
嗯。我的测试只显示了行号,并没有跳过任何行号。
-
我刚刚在 gist 中添加了一个目标 C 文件,该文件具有您应该使用的功能来代替您获得
calculatedIndex的代码
标签: ios objective-c uitableview gadbannerview