Advertisement
Guest User

LibraryViewController

a guest
Dec 12th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @implementation LibraryViewController
  2. @synthesize dataController = _dataController;
  3.  
  4. - (id)init{
  5.     UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  6.     self = [super initWithCollectionViewLayout:layout];
  7.     [self.collectionView setCollectionViewLayout:layout];
  8.     [self.collectionView setBounds:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  9.     self.dataController = [[LibraryDataController alloc] init];
  10.     [self.collectionView registerClass:[LibraryCell class] forCellWithReuseIdentifier:@"libraryCell"];
  11.     return self;
  12. }
  13. - (void)viewWillAppear:(BOOL)animated
  14. {
  15.     [self.navigationController setNavigationBarHidden:YES animated:animated];
  16.     [super viewWillAppear:animated];
  17.     [self.collectionView reloadData];
  18. }
  19.  
  20. - (void)viewWillDisappear:(BOOL)animated
  21. {
  22.     [self.navigationController setNavigationBarHidden:NO animated:animated];
  23.     [super viewWillDisappear:animated];
  24. }
  25.  
  26. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  27. {
  28.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  29.     if (self) {
  30.         // Custom initialization
  31.     }
  32.     return self;
  33. }
  34.  
  35. - (void)viewDidLoad
  36. {
  37.     [super viewDidLoad];
  38.     // Do any additional setup after loading the view.
  39. }
  40.  
  41. - (void)didReceiveMemoryWarning
  42. {
  43.     [super didReceiveMemoryWarning];
  44.     // Dispose of any resources that can be recreated.
  45. }
  46.  
  47. -(void)addPhoto:(NSString *)imagePath day:(NSInteger)day month:(NSInteger)month year:(NSInteger)year{
  48.     [self.dataController addPhoto:imagePath day:day month:month year:year];
  49. }
  50.  
  51. -(NSInteger)dataCount{
  52.     return [self.dataController listCount];
  53. }
  54.  
  55. #pragma mark -
  56. #pragma mark UICollectionViewDataSource
  57.  
  58. -(NSInteger)numberOfSectionsInCollectionView:
  59. (UICollectionView *)collectionView
  60. {
  61.     return 1;
  62. }
  63.  
  64. -(NSInteger)collectionView:(UICollectionView *)collectionView
  65.     numberOfItemsInSection:(NSInteger)section
  66. {
  67.     return [self.dataController listCount];
  68. }
  69.  
  70. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  71.     DetailViewController *detailView = [[DetailViewController alloc] init];
  72.     NSLog(@"%d",indexPath.row);
  73.     detailView.photoIndex = indexPath.row;
  74.     [self presentModalViewController:detailView animated:YES];
  75. }
  76.  
  77. - (CGSize)collectionView:(UICollectionView *)collectionView
  78.                   layout:(UICollectionViewLayout*)collectionViewLayout
  79.   sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  80.    
  81.     NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
  82.     return CGSizeMake(80, 120);
  83. }
  84.  
  85. - (UIEdgeInsets)collectionView:
  86. (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  87.     return UIEdgeInsetsMake(10, 10, 10, 10);
  88. }
  89.  
  90. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  91.     return 20.0;
  92. }
  93.  
  94. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
  95.                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97.     LibraryCell *myCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"libraryCell" forIndexPath:indexPath];
  98.    
  99.     UIImage *image;
  100.     int row = [indexPath row];
  101.    
  102.     NSString *photoPath = [self.dataController PhotoOrNotesAtIndex:row photoOrNotes:@"photo"];
  103.    
  104.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
  105.    
  106.     NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
  107.    
  108.     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:photoPath]; //add our image to the path
  109.    
  110.     image = [UIImage imageWithContentsOfFile:fullPath];
  111.    
  112.     if(image)
  113.         NSLog(@"success");
  114.    
  115.     myCell.imageView.image = nil;
  116.     myCell.imageView.image = image;
  117.    
  118.     return myCell;
  119. }
  120.  
  121. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement