在UIRefreshControl值更改后,UICollectionViewController项不响应单击操作

时间:2016-04-14 12:21:17

标签: ios objective-c uicollectionview uirefreshcontrol

我在里面创建了一个带有UICollectionViewController的单一屏幕应用程序。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    TestCollectionViewController *testCollectionViewController = [[TestCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:testCollectionViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = navigationController;

    [self.window makeKeyAndVisible];

    return YES;
}

TestCollectionViewController实现本身也很简单。它具有UICollectionViewDataSourceUICollectionViewDelegateUICollectionViewDelegateFlowLayout协议的实现。此外,还有UIRefreshControl的实现。

@implementation TestCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.collectionView.backgroundColor = [UIColor greenColor];
    self.collectionView.alwaysBounceVertical = YES;

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];

    refreshControl.tintColor = [UIColor orangeColor];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing..." attributes:[NSDictionary dictionaryWithObject:[UIColor orangeColor] forKey:NSForegroundColorAttributeName]];

    [refreshControl addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];

    [self.collectionView addSubview:refreshControl];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];

    return cell;
}

#pragma mark - UICollectionViewDelegate protocol methods

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%@ cell selected!", @(indexPath.item)] message:@"Everything is great!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertView show];
}

#pragma mark - UICollectionViewDelegateFlowLayout protocol methods

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.collectionView.bounds.size.width, 75.0f);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 1.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 1.0f;
}

- (void)refreshControlValueChanged:(UIRefreshControl *)sender
{
    [sender endRefreshing];
}

@end

当iPhone上的应用程序启动时,一切都很完美

enter image description here

当触发刷新控制操作一次时,集合视图第一个单元格不再响应任何操作。

以前有没有人遇到过这类问题?

0 个答案:

没有答案