UITableView header 中的 iAd 在 ViewController 解析网站时重新加载
iAd in UITableView header reloads when ViewController parses website
我想在我的 uitableview header 中添加一个广告,但是一旦 viewcontroller 完成对我们网页的解析,它就会重新加载并变为空白。我在这个页面中做了很多后台线程工作,特别是解析我们的网页并提取大量图像。让 viewController 静置一分钟左右后,广告 reappears/reloads 一切正常。当我滚动但并非所有单元格都已出现时,广告会不断重新加载(或者更确切地说,无法重新加载广告)。我究竟该如何避免它最初消失?这是我的相关代码:
#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
实例。每当滚动 header on-screen 以及调用 [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;
}
我想在我的 uitableview header 中添加一个广告,但是一旦 viewcontroller 完成对我们网页的解析,它就会重新加载并变为空白。我在这个页面中做了很多后台线程工作,特别是解析我们的网页并提取大量图像。让 viewController 静置一分钟左右后,广告 reappears/reloads 一切正常。当我滚动但并非所有单元格都已出现时,广告会不断重新加载(或者更确切地说,无法重新加载广告)。我究竟该如何避免它最初消失?这是我的相关代码:
#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
实例。每当滚动 header on-screen 以及调用 [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;
}