2014-03-05 3 views
0

내 앱에는보기가 2 개뿐입니다. 첫 번째보기는 컬렉션보기이며 이미지를 탭하면 MWPhotoBrowser으로 이동하여 전체 크기로 이미지를 봅니다.MWPhotoBrowser가 컬렉션보기에서 선택된 올바른 이미지를 표시하지 않습니다.

인터넷을 통해 ~ 100 개의 이미지를 가져 와서 NSMutableArray에 추가하고 있습니다. 문제가 있습니다. 처음으로의 앱을 실행하고 그리드에서 미리보기 이미지를 선택하면 마지막 이미지가 표시됩니다. 내가 선택한 것은 아닙니다. 첫 번째 시간에만 강조하기 때문에 처음으로 강조했습니다. 돌아가서 다른 이미지를 탭하면 선택한 이미지가 올바르게 표시됩니다. 아래는 이 어떻게 동작하는지 보여주는 스크린 샷입니다.

enter image description here

은 여기가 내가 이미지를 선택 처음으로 그리드의 마지막 이미지를 보여줍니다 이유는 알 수없는 코드

#import "UIImageView+WebCache.h" 
#import "ViewController.h" 
#import "MWPhotoBrowser.h" 
#import "Image.h" 
#import "ImageCell.h" 

@interface ViewController() <UICollectionViewDelegate, UICollectionViewDataSource, MWPhotoBrowserDelegate> 

@property (strong, nonatomic) NSMutableArray *images; 
@property (strong, nonatomic) NSMutableArray *browserImages; 
@property (strong, nonatomic) MWPhotoBrowser *browser; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.browser = [[MWPhotoBrowser alloc] initWithDelegate:self]; 
    self.browser.displayActionButton = YES; 
    self.browser.displayNavArrows = YES; 
    self.browser.displaySelectionButtons = NO; 
    self.browser.zoomPhotosToFill = YES; 
    self.browser.alwaysShowControls = YES; 
    self.browser.enableGrid = NO; 
    self.browser.startOnGrid = NO; 
    self.browser.wantsFullScreenLayout = NO; 

    [self.browser showNextPhotoAnimated:YES]; 
    [self.browser showPreviousPhotoAnimated:YES]; 

    [self loadImages]; 
} 

- (void)loadImages 
{ 
    self.images = [[NSMutableArray alloc] init]; 
    self.browserImages = [[NSMutableArray alloc] init]; 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", HOST_URL, @"All_Images.php"]]; 
    NSData *jsonResults = [NSData dataWithContentsOfURL:url]; 
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonResults options:0 error:NULL]; 

    NSDictionary *images = results[@"Images"]; 
    for (NSDictionary *img in images) { 
     Image *imageObj = [[Image alloc] init]; 
     imageObj.imageId = [img objectForKey:@"id"]; 
     imageObj.imageName = [img objectForKey:@"name"]; 

     // Get the full image path 
     NSString *fullImagePath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"full_image"]]; 
     imageObj.imagePath = fullImagePath; 

     // Get the thumbnail image path depending on the device 
     NSString *thumbnailPath; 
     if (DEVICE_IS_PAD) { 
      thumbnailPath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"thumbnail_ipad"]]; 
     } else { 
      thumbnailPath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"thumbnail_iphone"]]; 
     } 
     imageObj.imageThumbnail = thumbnailPath; 

     // Creates an object for each image and fill it with the retrieved info 
     [self.images addObject:imageObj]; 

     // This array stores the image paths for later use (displaying them in a photo browser) 
     MWPhoto *browserImage = [MWPhoto photoWithURL:[NSURL URLWithString:imageObj.imagePath]]; 
     browserImage.caption = imageObj.imageName; 
     [self.browserImages addObject:browserImage]; 
    } 
    [self.collectionView reloadData]; 
} 

#pragma mark - MWPhotoBrowserDelegate 
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser 
{ 
    return self.browserImages.count; 
} 

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index 
{ 
    if (index < self.browserImages.count) { 
     return self.browserImages[index]; 
    } 
    return nil; 
} 

#pragma mark - UICollectionViewDataSource 
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return self.images.count; 
} 

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"ImageCell"; 
    ImageCell *cell = (ImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 

    Image *image = self.images[indexPath.row]; 
    [cell.imageView setImageWithURL:[NSURL URLWithString:image.imageThumbnail] placeholderImage:[UIImage imageNamed:@"placeholder"]]; 

    return cell; 
} 

#pragma mark - UICollectionViewDelegate 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Opens the image browser 
    [self.browser setCurrentPhotoIndex:indexPath.row]; 
    [self.navigationController pushViewController:self.browser animated:YES]; 
} 

@end 

입니다. 문제가있는 데모 프로젝트를 업로드 했으므로 신속하게 살펴볼 수 있습니다. https://www.dropbox.com/s/tp6a67f83l0rzin/PhotoBrowserTest.zip

정말이 문제를 해결하는 데 도움을 주시면 감사하겠습니다.

감사합니다. 프로젝트의

답변

2

변경이 방법

- (NSUInteger)numberOfPhotos { 
    if (_photoCount == NSNotFound || _photoCount == 0) { 
     if ([_delegate respondsToSelector:@selector(numberOfPhotosInPhotoBrowser:)]) { 
      _photoCount = [_delegate numberOfPhotosInPhotoBrowser:self]; 
     } else if (_depreciatedPhotoData) { 
     _photoCount = _depreciatedPhotoData.count; 
     } 
    } 
    if (_photoCount == NSNotFound) _photoCount = 0; 
    return _photoCount; 
} 

하고 확인해야

- (id)initWithDelegate:(id <MWPhotoBrowserDelegate>)delegate { 
    if ((self = [self init])) { 
     _delegate = delegate; 
     [self setCurrentPhotoIndex:0]; 
    } 
    return self; 
} 

경우 _photoCount == 0, 당신은하지는 처음이 정보를 가지고 있기 때문에 _photoCount 것 0이 되겠지만 두 번째로 numberOfPhotosInPhotoBrowser가 오른쪽이됩니다

+0

고마워요! 그것은 잘 작동합니다. 나는 그 사실을 알았지 만 그 잘못은 내 자신의 코드에 있다고 생각했다. 어쨌든, 고마워! – Isuru