2015-06-03 43 views
1

頭重裝我想包括在我的UITableView頭廣告,但一旦視圖 - 控制完成分析我們的網頁重新加載和一片空白。我在這個頁面做了很多後臺線程工作,特別是解析我們的網頁並拉動了很多圖片。讓viewController靜置一分鐘左右後,廣告重新出現/重新加載,並且一切正常。當我滾動並且並非所有單元格都已出現時,廣告不斷重新加載(或者更確切地說,無法重新加載廣告)。我怎樣才能避免它最初消失?這裏是我的相關代碼:的iAd中的UITableView時的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 

回答

2

目前正在創建一個新的ADBannerView實例每次tableView:viewForHeaderInSection:被調用。只要標題在屏幕上滾動並且每當您撥打[self.tableView reloadData];時都會被調用。

- (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; 

} 
+0

啊:相反,只有在有沒有一個現有的創建一個新的ADBannerView實例。你100%正確。非常感謝。 – Jameson