Adding Admob ads to UITableView

梦想的初衷 提交于 2019-12-12 04:56:44

问题


This is the original code. I am a beginner in iOS development, how should I modify the code such that Admob ads will be loaded at the bottom of tableview? I tried to follow the tutorial from http://jmsliu.com/1207/add-google-admob-in-ios-apps.html , but can't get it to work.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.prefixArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    NSDictionary *d = self.prefixArray[indexPath.row];
    cell.textLabel.text = d[kMDTitleKey];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *d = self.prefixArray[indexPath.row];
    NSInteger entryID = [d[kMDIdentifierKey] integerValue];
    self.searchBar.text = d[kMDTitleKey];
    [self.db fetchDefinitionsWithID:entryID callback:^(NSDictionary *response) {
        NSString *HTML = [self.HTMLRenderer renderHTML:response];
        [self.webView loadHTMLString:HTML baseURL:nil];
        [self.searchDisplayController setActive:NO animated:YES];
    }];
}

回答1:


The tutorial you share uses the UITableView's title header to show an ad. Probably that's not a good idea since you'll probably would need to use those headers for your own purposes.

I understand you want to put ads at the bottom of the screen. In that case it would be a good idea to use an UIView below UITableView. In that case you'll need to change your UITableViewController for an UIViewController to be able to do that.

If you want to add the add at the bottom of the UITableView you should think of it as the cell N+1 (being N the size of self.prefixArray) and update your table callbacks to handle the special case correctly.




回答2:


You should add the advert view as the footerview for the tableview. Implement

  • (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

where you will create the advert view and return it as the footer view

dont put the advert view inside a cell, as it will be dequeued and its just the incorrect place for it.



来源:https://stackoverflow.com/questions/29563670/adding-admob-ads-to-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!