我是集合视图的新手,想知道这是否是创建它们的最佳方式,我也想了解一下从哪里到segue到启用分页的详细视图的建议。
#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
是详细信息视图的另一个集合视图,或者启用了分页的滚动视图,因为我不确定如何实现它?我也希望能够在顶部导航栏上有一个标题?
提前致谢!
答案 0 :(得分:1)
我可以在你的代码中发现一些编码错误。它会导致错误。 marbleImages
是一个带有字符串对象的arry
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原生照片应用。要获得相同的感觉,您应该使用scrollview并启用分页,甚至可以使用集合视图。一个简单的实现是使用集合视图。如果您调整CollectionViewFlowLayout的属性,您可以实现这一点。