如何阻止scrollViewDidScroll在View加载时调用?

时间:2014-03-21 13:10:30

标签: iphone objective-c uitableview

我有UITableView,它的代理是通过xib设置的,如下图所示。

enter image description here

我在方法中添加了一些LOAD MORE CODE,这样当用户向下滚动时,它会向服务器发送请求并加载更多值。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"Veer Suthar Scrolling the view");


    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.bounds;
    CGSize size = scrollView.contentSize;
    UIEdgeInsets inset = scrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

    float reload_distance = 15;
    if(y > h + reload_distance)
    {
        //Call the Method to load More Data...
        AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

        if ([appDelegate.reach isReachable])
        {
            if (onceCallTheLoadMore) {

                onceCallTheLoadMore = NO;

                int startCountValue = [vouchersArray count]+1;
                int endCountValue = [vouchersArray count] + 10;


                startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d", startCountValue]];
                endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d", endCountValue]];

                NSString *totalCount = [[NSUserDefaults standardUserDefaults] valueForKey: @"totalCountOfFeaturedVouchers"];


                int totalCountValue = [totalCount intValue];


                if (endCountValue > totalCountValue) {
                    ////
                    onceCallTheLoadMore = YES;

                }else{

                    [self featuredWebService];

                }
                //Veer, call here method for load more
            }else{


            }
        }
    }
}

此处在View Loads中调用此方法。

如何在视图加载时阻止它调用。

我试过了  [self.tableview setDelegate:nil]; //在ViewWillAppear中使用,再在ViewDidLoad中使用,设置为SELF。

但它不能正常工作。

ViewDidLoad COde

- (void)viewDidLoad
{

    [super viewDidLoad];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
        {

            if( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON ){

                [self.tableView setFrame:CGRectMake(0, 64,320, 390)];


            }else{

                NSLog(@"ViewDidAppear is laoded ");

                [self.tableView setFrame:CGRectMake(0, 64,320, 455)];


            }

        }
    }


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
    {
        [self.view setBackgroundColor:[UIColor clearColor]];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nev_top_right_title.png"] forBarMetrics:UIBarMetricsDefault];

        UIButton *locationBtn     = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *locationBtnImage = [UIImage imageNamed:@"map.png"]  ;
        //[backBtn setTitle:@"Back" forState:UIControlStateNormal];
        [locationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        //    [backBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:@"font"];
        [locationBtn setBackgroundImage:locationBtnImage forState:UIControlStateNormal];
        [locationBtn addTarget:baseViewController action:@selector(locationBtnPressed) forControlEvents:UIControlEventTouchUpInside];
        locationBtn.frame = CGRectMake(0, 0, 41, 35);
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:locationBtn] ;
        self.navigationItem.leftBarButtonItem = leftButton;

        UIButton *searchBtn     = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *searchBtnImage = [UIImage imageNamed:@"search.png"]  ;
        //[backBtn setTitle:@"Back" forState:UIControlStateNormal];
        [searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        //    [searchBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:@"font"];
        [searchBtn setBackgroundImage:searchBtnImage forState:UIControlStateNormal];
        [searchBtn addTarget:baseViewController action:@selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];
        searchBtn.frame = CGRectMake(0, 0, 39, 30);
        UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:searchBtn] ;
        self.navigationItem.rightBarButtonItem = rightBtn;


        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];

        UIImageView *logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"title_logo_ipad.png"]];
        [logo setFrame:CGRectMake(62.0, -6.0, 62.0, 33.0)];
        [titleView addSubview:logo];
        //[titleView addSubview:title];

        self.navigationItem.titleView = logo;
        [self.navigationController.navigationBar subviews];
        [titleView release];
        [leftButton release];
        [rightBtn release];
        //    [title release];
        [logo release];


        [(UITableView *)[self view] setSeparatorColor:[UIColor clearColor]];
    }
    else {
        [(UITableView *)[self view] setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        UIEdgeInsets inset = UIEdgeInsetsMake(44, 0, 0, 0);
        self.tableView.contentInset = inset;
        self.tableView.scrollIndicatorInsets = inset;
        [self.view setBackgroundColor:[UIColor clearColor]];
    }





    onceCallTheLoadMore = YES;


    int startCountValue = [vouchersArray count]+1;
    int endCountValue = [vouchersArray count] + 10;


    startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d", startCountValue]];
    endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d", endCountValue]];


    self.vouchersArray = [[NSMutableArray alloc] initWithCapacity:0];

    self.parser = [[[TBXMLParser alloc] init] autorelease];
    parser.delegate = self;

    self.arrFeaturedVouchers = [[NSMutableArray alloc]initWithCapacity:0];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

    if ([appDelegate.reach isReachable])
    {

        [self performSelector:@selector(featuredWebService) withObject:nil afterDelay:0.1];


    }



}

ViewWIllAPpear代码

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view setBackgroundColor:[UIColor clearColor]];

    if(voucherType == kVoucherTypeFeatured)
    {
        UIButton *locationBtn     = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *locationBtnImage = [UIImage imageNamed:@"b_icon.png"]  ;
        //[backBtn setTitle:@"Back" forState:UIControlStateNormal];
        [locationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        //    [backBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:@"font"];
        [locationBtn setBackgroundImage:locationBtnImage forState:UIControlStateNormal];
        // Edited by Sugam on 19 Dec 2012
        [locationBtn addTarget:[[UIApplication sharedApplication] delegate] action:@selector(locationBtnPressed) forControlEvents:UIControlEventTouchUpInside];
        locationBtn.frame = CGRectMake(0, 0, 41, 35);
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:locationBtn] ;
        self.navigationItem.leftBarButtonItem = leftButton;
        [leftButton release];
    }
    UIButton *searchBtn     = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *searchBtnImage = [UIImage imageNamed:@"seach_top.png"]  ;
    //[backBtn setTitle:@"Back" forState:UIControlStateNormal];
    [searchBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    //    [searchBtn setValue:[UIFont boldSystemFontOfSize:13.0] forKey:@"font"];
    [searchBtn setBackgroundImage:searchBtnImage forState:UIControlStateNormal];
    // Edited by Sugam on 19 Dec 2012
    [searchBtn addTarget:[[UIApplication sharedApplication] delegate] action:@selector(searchBtnPressed) forControlEvents:UIControlEventTouchUpInside];

    searchBtn.frame = CGRectMake(0, 0, 39, 30);
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:searchBtn] ;
    self.navigationItem.rightBarButtonItem = rightBtn;


    UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 25.0)];


    UIImageView *logo;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {


        logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"title_logo.png"]];
    }
    else
    {


        logo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"title_logo_ipad.png"]];
    }

    [logo setFrame:CGRectMake(62.0, -6.0, 62.0, 33.0)];
    [titleView addSubview:logo];
    //[titleView addSubview:title];

    self.navigationItem.titleView = logo;
    [self.navigationController.navigationBar subviews];
    [titleView release];
    //    [title release];
    [logo release];

    [rightBtn release];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {

        [self.tabBarController showTabBar];
    }


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
        {

            if( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON ){

                [self.tableView setFrame:CGRectMake(0, 64,320, 390)];

            }else{

                NSLog(@"ViewDidAppear is laoded ");
                [self.tableView setFrame:CGRectMake(0, 64,320, 455)];

            }

        }
    }


    dispatch_async(dispatch_get_main_queue(), ^{
        /* Do somthing here with UIKit here */


        _iconDownloader = [[ImageCache alloc] init];
        _iconDownloader.delegate = self;



        startCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"0"]];
        endCount = [[NSString alloc] initWithString:[NSString stringWithFormat:@"10"]];



        [Flurry logEvent:@"Featured Section Browse" timed:YES]; //How long the user has spent on the app

        [self getLocation];


        AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication]delegate];

        self.arrFeaturedVouchers = (NSMutableArray*) [app.dbCommunicator getAllFeaturedVouchers];

        if ([self.arrFeaturedVouchers count]>0)
        {
            [self.vouchersArray removeAllObjects];

            for (int i = 0; i<[self.arrFeaturedVouchers count]; i++)
            {
                FeaturedVoucher *aFeaturedVoucher = [self.arrFeaturedVouchers objectAtIndex:i];


                Voucher *aVoucher = [app.dbCommunicator getVoucherInfoForCampaignId:aFeaturedVoucher.featuredCampaignId];

                if(aVoucher)
                {
                    [self.vouchersArray addObject:aVoucher];
                    ////
                }
            }

            [self.arrFeaturedVouchers removeAllObjects];

            [self.tableView reloadData];
        }

    });


}

任何建议

先谢谢。

1 个答案:

答案 0 :(得分:2)

viewWillApear方法中,您有[self.tableView reloadData] - 这会导致scrollView调用其委托scrollViewDidScroll