0

我是集合视图的新手,想知道这是否是创建它们的最佳方式,我还想获得一些关于从那里转到启用分页的详细视图的建议。

#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)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"detailView"]) {
        DetailViewController *destViewController = segue.destinationViewController;
        NSIndexPath *indexPath = [self.collectionView ];
        destViewController.recipeImageName = [marbleImages[indexPath.section] 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 recipeImageView;
@synthesize recipeImageName;

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

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

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

@end

是详细视图的另一个集合视图,还是启用分页的滚动视图,因为我不确定如何实现这一点?我也希望能够在顶部导航栏上有一个标题?

提前致谢!

4

1 回答 1

1

我可以在您的代码中找到一些编码错误。它会导致错误。 marbleImages是一个包含字符串对象的数组

marbleImages = [NSArray arrayWithObjects:@"arabescato.jpg",...];

但是在prepareForSegue:你做的像

destViewController.recipeImageName = [marbleImages[indexPath.section] objectAtIndex:indexPath.row]; 

此代码需要一个数组对象数组。上面的代码可以转换为

 NSString *obj = [marbleImages[indexPath.section]] // It will return string only since you are storing string
[obj objectAtIndex:indexPath.row]// This will cause error since NSString does not have a method objectAtIndex:  

我希望你只有一个部分(我没有找到 noOfSections 方法)。所以喜欢

    destViewController.recipeImageName = [marbleImages objectAtIndex:indexPath.row];

详细视图的建议

我认为您正在尝试模仿 iPhone 的原生照片应用程序。要获得相同的感觉,您应该使用启用分页的滚动视图,甚至可以使用集合视图。一个简单的实现是使用集合视图。如果您调整 CollectionViewFlowLayout 的属性,您可以实现这一点。

于 2013-05-17T11:21:05.207 回答