0

我正在慢慢地想复制一个 iPhone 照片画廊,其中收藏视图与细节视图相结合,我希望细节视图像照片画廊一样具有分页功能。到目前为止,我的代码似乎已加载,但随后出现错误。我有豁免断点,断点 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

非常感谢您提供的任何帮助,我真的很感激。

4

1 回答 1

1

您传递的是图像的路径,而不是 DetailViewController 的图像,因此您的 viewDidLoad 必须是:

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

这应该是一个很好的命名课程——如果不是一个图像(或 img),就不要称它为图像(或 img),这只会造成混淆。将其称为更具描述性的名称,例如 imageName 或 imagePath。

于 2013-05-20T16:55:53.267 回答