开发者

Showing adMob or adWhirl ads correctly in an UITableView

开发者 https://www.devze.com 2023-03-15 03:02 出处:网络
I have a problem with implementing AdMob in my UITableView. I have an array (newsItems) that contains news-feeds from different websites. The array is sorted by the date of the newsitem. I want to dis

I have a problem with implementing AdMob in my UITableView. I have an array (newsItems) that contains news-feeds from different websites. The array is sorted by the date of the newsitem. I want to display the newsitems in a tableview, but in the free version of the app I want an advertisement from AdMob to show in every 10th tableviewcell.

I used the code from an other answer to add the ads to the tableview:

if (0 == (row % 10))  {    

//cells with an add 
static NSString *MyIdentifier;
NSInteger row = [indexPath row];
MyIdentifier = [NSString stringWithFormat:@"AdCell%d", row];

cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];      
if (cell == nil) {         
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];     
}

[cell.contentView addSubview:[AdMobView requestAdWithDelegate:self]];
return cell;

} else {      
// the code to create a standard cell (simplified)
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

NSString *cellValue = [newsItems objectAtIndex:indexPath.row];
cell.text = cellValue;

}

I know this is not correct. By using “[newsItems objectAtIndex:indexPath.row]” the table is populated by going through the array and adding the newsitem at the index to the m开发者_开发知识库atching row. This means that if an add should be added to row 10, the corresponding newsItem at row 10 of the array is not ignored and overwritten by the AdWhirl-ad.

In stead I want that newsitem to be added to the row beneath the ad. Is this possible?


I had a ListObject class which would store the data about that cell (eg. Title, thumbnail, desc etc.) so I added a field: BOOL isAd and then just added a new ListObject with isAd = YES every 10 objects and made sure that my cell creation and height logic knew what to do with Ad cells. This way, you have a list data object for each Ad view in your table.

0

精彩评论

暂无评论...
验证码 换一张
取 消