Guest User

Untitled

a guest
Jan 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. @interface MainViewController : UIViewController
  2. {
  3. @private
  4. UICollectionView *_collectionView;
  5. NSMutableArray *_results; // data source array
  6. }
  7. @end
  8.  
  9. @interface MainViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
  10. @property (nonatomic, retain) UICollectionView *collectionView;
  11. @property (nonatomic, retain) NSMutableArray *results;
  12. @end
  13.  
  14. @implementation MainViewController
  15.  
  16. @synthesize collectionView = _collectionView;
  17. @synthesize results = _results;
  18.  
  19. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  20. {
  21. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  22. if (self) {
  23. // some init stuff - nothing to do with collection view.
  24. }
  25.  
  26. return self;
  27. }
  28.  
  29. - (void)loadView
  30. {
  31. self.results = [NSMutableArray array];
  32. UIImage *image1 = [UIImage imageNamed:@"img1.jpg"];
  33. UIImage *image2 = [UIImage imageNamed:@"img2.jpg"];
  34. [self.results addObject:image1];
  35. [self.results addObject:image2];
  36.  
  37. self.collectionView.dataSource = self;
  38. self.collectionView.delegate = self;
  39.  
  40. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  41.  
  42. UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
  43. self.collectionView = collectionView;
  44.  
  45. [self.view addSubview:self.collectionView];
  46.  
  47. [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
  48. [self.collectionView reloadData];
  49.  
  50. }
  51.  
  52. - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section
  53. {
  54. return [self.results count];
  55. }
  56.  
  57. - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
  58. {
  59. return 1;
  60. }
  61.  
  62. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  63. {
  64. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  65. cell.backgroundColor = [UIColor colorWithPatternImage:[self.results objectAtIndex:indexPath.row]];
  66. return cell;
  67. }
  68.  
  69.  
  70. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  71. UIImage *image = [self.results objectAtIndex:indexPath.row];
  72. return CGSizeMake(image.size.width, image.size.height);
  73. }
  74.  
  75. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  76. return UIEdgeInsetsMake(50, 20, 50, 20);
  77. }
  78.  
  79. self.collectionView = collectionView;
  80. self.collectionView.dataSource = self;
  81. self.collectionView.delegate = self;
Add Comment
Please, Sign In to add comment