ios - 当 ViewController 解析网站时,UITableView header 中的 iAd 会重新加载

标签 ios objective-c uitableview iad

我想在我的 uitableview 标题中包含一个广告,但是一旦 viewcontroller 完成解析我们的网页,它就会重新加载并变为空白。我在这个页面中做了很多后台线程工作,特别是解析我们的网页并提取大量图像。让 viewController 静置一分钟左右后,广告重新出现/重新加载,一切正常。当我滚动但并非所有单元格都已出现时,广告会不断重新加载(或者更确切地说,无法重新加载广告)。我究竟该如何避免它最初消失?这是我的相关代码:

#import "RosterTableTableViewController.h"
#import "TFHpple.h"
#import "RosterListing.h"
#import "RosterListingCellTableViewCell.h"
#import "PlayerDetailViewController.h"
#import <iAd/iAd.h>

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

@interface RosterTableTableViewController () <ADBannerViewDelegate>
{
    BOOL _bannerIsVisible;
    ADBannerView *_adBanner;
}

@property (nonatomic, strong) NSMutableArray *rosters;
@property NSCache *imageCache;

@end

@implementation RosterTableTableViewController

- (void) loadRoster
{
    NSURL *RosterURL = [NSURL URLWithString:@"myURL"];

    ...

    self.rosters = rosterItems;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.lancers.myqueue", 0);
    //dispatch in background
    dispatch_async(backgroundQueue, ^{
        //execute long operation in background thread
        //dispatch in main thread after long operation is finish
        [self loadRoster];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData]; });
    });

    // Load the Cell NIB file
    ...
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get a new or recycled cell
    RosterListingCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RosterCell" forIndexPath:indexPath];

    RosterListing *thisRosterListing = [self.rosters objectAtIndex:indexPath.row];
    cell.playerNumberLabel.text = [NSString stringWithFormat:@"#%@",thisRosterListing.playerNumber];
    cell.playerNameLabel.text = thisRosterListing.playerName;

    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    cell.imageView.clipsToBounds = YES;

    UIImage *playerImage = [self.imageCache objectForKey:thisRosterListing.playerImageURL];
    cell.imageView.image = playerImage;
    if (playerImage == nil) {

        NSURLSessionConfiguration *sessionConfig =
        [NSURLSessionConfiguration defaultSessionConfiguration];

        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
        thisRosterListing.playerImageURL = [thisRosterListing.playerImageURL stringByReplacingOccurrencesOfString:@"small" withString:@"medium"];
        NSURLSessionDataTask *imageData = [session dataTaskWithURL:[NSURL URLWithString: thisRosterListing.playerImageURL]
                                                completionHandler:^(NSData *data,
                                                                    NSURLResponse *response,
                                                                    NSError *error) {
                                                    // handle NSData
                                                    UIImage *image = [UIImage imageWithData:data];
                                                    thisRosterListing.image = image;
                                                    [self.imageCache setObject:image forKey:thisRosterListing.playerImageURL];
                                                    cell.imageView.image = image;
                                                    dispatch_async(dispatch_get_main_queue(), ^{
                                                        [self.tableView reloadData];
                                                    });
                                                }];
        [imageData resume];
    }


    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 65)];
    view.backgroundColor = [UIColor clearColor];
    _adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    //_adBanner.delegate = self;
    _adBanner.backgroundColor = [UIColor clearColor];
    [view addSubview:_adBanner];

    return view;

}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 65;
}

@end

最佳答案

当前,每次调用 tableView:viewForHeaderInSection: 时,您都会创建一个新的 ADBannerView 实例。每当标题在屏幕上滚动时以及您调用 [self.tableView reloadData]; 时都会调用它。相反,只创建一个新的 ADBannerView 实例如果还没有现成的实例:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 65)];
    view.backgroundColor = [UIColor clearColor];

    if (_adBanner == nil)
    {
        _adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    }

    //_adBanner.delegate = self;
    _adBanner.backgroundColor = [UIColor clearColor];
    [view addSubview:_adBanner];

    return view;

}

关于ios - 当 ViewController 解析网站时,UITableView header 中的 iAd 会重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30625427/

相关文章:

iOS:生成随机对象 ID

使用 ZBarSDK 时 iPhone 相机失去自动对焦

ios - 从 UITableView 中删除自定义 View

ios - 如何在 UITableView 仍在滚动时在 tableHeaderView 上启用触摸事件?

iphone - 在 UITableView 中实现 Prev./Next 按钮,使第一响应者成为不可见单元格中的 UITextField

未安装 iOS TestFlight 共享扩展

ios - 具有自定义 UTI 的 iCloud 文档选取器

iOS:文本未显示在旋转的 UITextField 内

iphone - 在 iOS 中播放嵌入的 youtube 视频后返回特定 View

objective-c - 在 NSView - Cocoa 中以编程方式创建 NSScrollView