我在里面创建了一个带有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
实现本身也很简单。它具有UICollectionViewDataSource
,UICollectionViewDelegate
和UICollectionViewDelegateFlowLayout
协议的实现。此外,还有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上的应用程序启动时,一切都很完美
当触发刷新控制操作一次时,集合视图第一个单元格不再响应任何操作。
以前有没有人遇到过这类问题?