Search code examples
iosuicollectionviewmwphotobrowser

Add photos to MWPhotoBrowser


I have my first view controller imageResults and in that calls the MWPhotoBrowser with the code below. I see it displays a new view controller using the data from self.photos. I've tried a few options for adding more data but none successful. I want to be able to load more data in the new view and add it to the photobrowser, the adding more data part is already don't, now how do i set the current browser to load the new array without loosing the current position?

    -(void)mvTouched:(NSIndexPath *)indexPath {
    NSLog(@"Arr:%lu  Url:%lu", [titlesMutable count], [mediaUrlDict count]);

    NSMutableArray *tmpPhotos = [NSMutableArray array];
    for(NSString *titleString in titlesMutable) {
        NSString *url = [mediaUrlDict objectForKey:titleString];
        MWPhoto *currentPhoto = [MWPhoto photoWithURL:[NSURL URLWithString:url]];
        currentPhoto.caption=[NSString stringWithFormat:@"%@", titleString];
        [tmpPhotos addObject:currentPhoto];
        //[tmpPhotos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:url]]];

    }
    self.photos = [NSArray arrayWithArray:tmpPhotos];


    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];

    // Set options
    browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    browser.displayNavArrows = YES; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    browser.alwaysShowControls = YES; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    browser.edgesForExtendedLayout = YES;
    browser.extendedLayoutIncludesOpaqueBars = YES;

    // Optionally set the current visible photo before displaying
    //[browser setCurrentPhotoIndex:1];
    [browser setCurrentPhotoIndex:indexPath.row];

    self.navigationController.navigationBar.hidden=NO;
    // Present
    [self.navigationController pushViewController:browser animated:YES];

    // Manipulate
    [browser showNextPhotoAnimated:YES];
    [browser showPreviousPhotoAnimated:YES];
    //[browser setCurrentPhotoIndex:1];
    [browser setCurrentPhotoIndex:indexPath.row];


    [browser reloadData];
}
-(void)loadMore {
    [self addDataToArray];
    //[self mvTouched:nil];



    [self.collectionView reloadData];

}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}
- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser actionButtonPressedForPhotoAtIndex:(NSUInteger)index {
    // Do your thing!

    NSLog(@"ACTION!!!");
}

Solution

  • Calling [self.collectionView reloadData] will only reload the current view's collection instead of the MWPhotoBrowser's. You need to call the browsers reloadData function for this to work like so:

    [browser reloadData]
    

    Note that there is currently an issue with the browsers collection view not displaying the new photos after calling this method. I managed to work around this by adding this code to the reloadData method found in the MWPhotoBrowser.m file:

    if (_gridController) {
        [_gridController.collectionView reloadData];
    }