2013-05-20 120 views
0

我正在慢慢地走出我的想法,試圖複製一個iPhone照片庫與集合視圖segueing到一個詳細信息視圖,我希望細節視圖有分頁就像照片庫確實。我到目前爲止的代碼似乎加載,但然後有錯誤。我有豁免斷點和斷點1.1崩潰點是行self.imageView.image = img ;.除了(lldb)之外,這並沒有說明日誌上的錯誤。從集合視圖繼續查看詳細視圖問題

我想知道我是否可以借用某人的天才來指出我出錯的地方,或許正確嗎?我也想借此爲每個圖像標題對面的詳細視圖,但不知道如何/在哪裏添加此

我的代碼是:

#import "MarbleCollectionViewController.h" 
#import "DetailViewController.h" 

@interface MarbleCollectionViewController() { 

    NSArray *marbleImages; 
} 

@end 

@implementation MarbleCollectionViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    marbleImages = [NSArray arrayWithObjects:@"arabescato.jpg", 
        @"bianco-carrara.jpg", 
        @"botticino-classico2.jpg", 
        @"Calacatta_Oro.jpg", 
        @"crema-marfil-3.jpg", 
        @"crema-valencia.jpg", 
        @"emperador-dark.jpg", 
        @"jura-beige.jpg", 
        @"nero-marquina.jpg", 
        @"perlato-olympo.jpg", 
        @"rojo-alicante_marbleGRP1.jpg", nil]; 
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
    return marbleImages.count; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIImageView *marbleImageView = (UIImageView *)[cell viewWithTag:100]; 
    marbleImageView.image = [UIImage imageNamed:[marbleImages objectAtIndex:indexPath.row]]; 

    return cell; 
} 

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"detailView"]) { 
     DetailViewController *destViewController = segue.destinationViewController; 
     NSIndexPath *indexPath = [[self.collectionView indexPathsForSelectedItems] objectAtIndex:0]; 
     destViewController.img = [marbleImages objectAtIndex:indexPath.row]; 
     [self.collectionView deselectItemAtIndexPath:indexPath animated:NO]; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

和詳細信息視圖是:

#import "DetailViewController.h" 

@interface DetailViewController() 

@end 

@implementation DetailViewController 

@synthesize imageView, img; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.imageView.image = img; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

非常感謝您的任何幫助,您可以提供的是爲了我真的很感激。

+0

你沒有說你正在得到哪個錯誤 – meda

+0

我有豁免斷點和斷點1.1崩潰點是行self.imageView.image = img ;.這並沒有說明日誌上的錯誤,除了(lldb) – David

回答

1

你傳遞一個路徑的圖像,而不是像你DetailViewController,讓您的viewDidLoad必須是:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.imageView.image = [UIImage imageNamed:img]; 
} 

這應該是命名一個很好的教訓 - 不叫什麼一個圖像(或img),如果它不是一個,它只是使混亂。稱之爲更像imageName或imagePath的描述性內容。

+0

感謝您的幫助到目前爲止!儘管如此,仍然沒有工作,說 - 不兼容的指針類型發送'UIImage * __強'參數類型'NSString *' – David

+0

最後也得到一個錯誤報告 - ***由於未捕獲異常'NSRangeException',原因:'終止應用程序' *** - [__ NSArrayI objectAtIndex:]:索引0超出空數組的邊界' – David

+0

@David,img如何在DetailViewController中聲明?它應該被聲明爲一個字符串。範圍異常錯誤是我想的另一個問題。該錯誤發生在哪條線上? – rdelmar