从集合视图到详细视图问题

时间:2013-05-20 16:14:16

标签: iphone ios uicollectionview

我正在慢慢想着尝试复制一个带有收集视图的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

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

1 个答案:

答案 0 :(得分:1)

您将路径传递给图像,而不是将图像传递给DetailViewController,因此您的viewDidLoad需要:

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

这应该是命名的一个很好的教训 - 不要把某个图像(或img)称为某个图像(或img),如果它不是一个,它只会造成混淆。称之为像imageName或imagePath更具描述性的东西。

相关问题