0

Hi there i'm working on a new app that has a CollectionView which loads data from JSON and when clicked on specific Cell goes to a Detail/FullScreenView. So far so good, but now i wanted to implement swipe on it. So the idea was click/tap specific Cell -> get fullscreenView -> swipe through all the images on FullScreenView. The point is my fullscreenView is working and showing the image selected previously on CollectionView, but the swipe is not working, or well it is getting recognized but not changing image. some help would be pretty good. Thanks in advance!

My fotos.h (NSObject)

   @property NSString* Foto;
   @property NSString* Descricao;
   @property NSData* FotoImagem;

My globals.h

   + (NSMutableArray*) getFotos;
   + (void) setFotos: (NSMutableArray*) fotos;

globals.m

   + (NSMutableArray*) getFotos
{
    if (_fotos == nil)
    _fotos = [NSMutableArray new];

   return _fotos;
}

+ (void) setFotos:(NSMutableArray*) fotos
{
   _fotos = fotos;
}

CollectionView.h

@property IBOutlet UICollectionView *collectionView;

- (IBAction)back:(id)sender;

CollectionView.m

- (void)loadJSON
{

    NSMutableArray* fotos = [[NSMutableArray alloc] init];
    NSError *erro;
    NSData* jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URL_json] options:NSDataReadingUncached error:&erro];

    NSLog(@"Por alguma razão não consegues carregar os dados!");

    NSString* jsonStringData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    if (jsonData == nil) {
    }

    else {

        NSError *erro1;
        NSArray *listafotos = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&erro1];

        NSInteger totalfotos = [listafotos count];
        for (NSDictionary* fot in listafotos){

            ExpSFotos* f1 = [ExpSFotos new];
            f1.Foto = [fot objectForKey:@"Foto"];
            f1.Descricao = [fot objectForKey:@"Descricao"];

            NSString* fotoX = f1.Foto;

            NSString* urlFoto = [[NSString alloc] initWithFormat:@"%@%@", URL_fotos, fotoX];
            NSData* fotoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlFoto]];
            f1.FotoImagem = fotoData;

            [fotos addObject:f1];

        }
        [ExpSGlobals setFotos:fotos];
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSMutableArray *fotos = [ExpSGlobals getFotos];
    return fotos.count;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ExpSCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];


    if (cell == nil) {

    } else {

        NSMutableArray *fotos = [ExpSGlobals getFotos];
        ExpSFotos *f = [fotos objectAtIndex:indexPath.row];
        cell.foto.image = [UIImage imageWithData:f.FotoImagem];

    }

    return cell;
}


NSIndexPath* lastIndexPath;

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    lastIndexPath = indexPath;
    [self performSegueWithIdentifier:@"fullScreen" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"fullScreen"])
    {

        NSMutableArray* fotos = [ExpSGlobals getFotos];
        ExpSFotos* f = [fotos objectAtIndex:lastIndexPath.row];

        ExpSFullScreenViewController* fullScreen = (ExpSFullScreenViewController*)segue.destinationViewController;
        fullScreen.imageToLoad = [UIImage imageWithData:f.FotoImagem];

        fullScreen.detailsDataSource = [[NSArray alloc] initWithArray:fotos];
        fullScreen.detailIndex = lastIndexPath.row;
    }

}


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

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

FullScreen.h

@property UIImage *imageToLoad;

@property IBOutlet UIImageView *full;

@property (nonatomic) NSString *selectedObject;

@property NSMutableArray *arr;

@property (nonatomic) NSArray *detailsDataSource;
@property int detailIndex;

FullScreen.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.full.image = self.imageToLoad;
    [full setUserInteractionEnabled:YES];

    UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedLeft:)];
    leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftGesture];

    UISwipeGestureRecognizer *rightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedRight:)];
    rightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightGesture];
}

- (void)swipeDetectedRight:(UISwipeGestureRecognizer *)sender
{
    if (detailIndex != 0)
        detailIndex --;

    NSLog(@"Swipe Right Detected");

    full.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Foto"]];

    NSLog(@"%@", full); 
}

- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
    if (detailIndex < [detailsDataSource count])
        detailIndex ++;

    full.image = [UIImage imageNamed:[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Foto"]];

    NSLog(@"Swipe Left Detected");
    NSLog(@"%@", full);

}

I'm kinda new to this language, don't have that much knowledge on other languages either, but with some effort and research i've been able to achieve what i'm looking for till now. Some help would be appreciated!

PCorreia
  • 1
  • 1
  • Instead of (detailIndex != [detailsDataSource count]) you should check if (detailIndex < [detailsDataSource count]) – Sagito Oct 14 '14 at 14:33
  • Edited, thanks for the correction. Still no images on swipe! – PCorreia Oct 14 '14 at 14:36
  • When you load the initial image, you are using the `FotoImagem` property, whereas later you are using the `Foto` property. Is that correct? – Marcus Adams Oct 14 '14 at 14:45
  • Then you need to step through and test this because you're doing it differently. Also, when you pass the `fotos` array, why are you copying it again before you pass it? – Marcus Adams Oct 14 '14 at 14:49
  • Where exactly have i done that? i'm way to tired and completely lost right now to be honest. – PCorreia Oct 14 '14 at 15:17

0 Answers0