2015-06-03 5 views
1

Я хочу включить объявление в свой заголовок uitableview, но как только viewcontroller завершит разбор нашей веб-страницы, он перезагрузится и опустится. Я делаю много фоновой работы на этой странице, в частности, анализируя нашу веб-страницу и вытягивая много изображений. После того, как 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];. Вместо этого, только создать новый экземпляр 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; 

} 
+0

Ааа. Ты на 100% прав. Огромное спасибо. – Jameson

Смежные вопросы