سؤال

i implemented a UIRefreshControl for UICollectionView so that user can pull to refresh the content in UICollectionView. I am testing on the iPad simulator.

On the first attempt, I'm able to pull and refresh the content. However, I notice that the loading icon is still loading and doesn't stop. On my second attempt with the loading icon still showing, I pulled to refresh but it fails to call my selector(refreshCollectionAction).

Here is what I did:

-(void)viewDidLoad  
{
    // Do any additional setup after loading the view.
    [super viewDidLoad];

    // Register collectionView pull down to refresh
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refreshCollectionAction)
             forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:refreshControl];
    self.collectionView.alwaysBounceVertical = YES; 
.....  
}

-(void)refreshCollectionAction 
{
    NSLog(@"refresh collection action");

    // Empty product Items Array
    [[ProductStore getInstance] emptyProductInStore];

    NSInteger numOfProductInStore = [[[ProductStore getInstance] allProductItems] count];
    if (numOfProductInStore <= 0) {
        // Fetch data from webservice and reload collectionView
        [self fetchFeed:@""];
    } 
}

Am I missing some configurations? fetchFeed will request the data from web services. I have verified that the webservice still works.

هل كانت مفيدة؟

المحلول

[self.refreshControl endRefreshing];

Call this method at the end of any refresh operation (whether it was initiated programmatically or by the user) to return the refresh control to its default state. If the refresh control is at least partially visible, calling this method also hides it. If animations are also enabled, the control is hidden using an animation. UIRefreshControl Class Reference

نصائح أخرى

@interface ProductSearchViewController ()

@property(nonatomic)UIRefreshControl *refreshControl;

@end

- (void)viewDidLoad
{
    // Do any additional setup after loading the view.
    [super viewDidLoad];

    // Register collectionView pull down to refresh
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refreshCollectionAction)
             forControlEvents:UIControlEventValueChanged];
    [self.collectionView addSubview:self.refreshControl];
    self.collectionView.alwaysBounceVertical = YES;

...
}

-(void)refreshCollectionAction
{
    NSLog(@"refresh collection action");

    // Empty product Items Array
    [[posProductStore getInstance] emptyProductInStore];

    NSInteger numOfProductInStore = [[[posProductStore getInstance] allProductItems] count];
    if (numOfProductInStore <= 0) {
        [self fetchFeed:@""];
    }
    [self.refreshControl endRefreshing];
}

so basically i declare refreshControl as a class variable. as Neil mentioned, i added [self.refreshControl endRefreshing] at the end of method -(void)refreshCollectionAction.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top