iOS 6 Collection View Controller多个动态单元格

时间:2013-09-12 16:06:20

标签: iphone xcode uicollectionviewcell collectionview

我只有一个星期进入Xcode,试图学习一些东西,所以我可以为一个项目做贡献..

到目前为止,我一直在研究教程并制作了一些内容:)但我还远未结束。我希望有人能理解我会问的是什么......

我正在创建一个将从服务器获取数据的应用程序,数据将包括缩略图图像,图像和文本。现在我有一个集合视图控制器,一个有3个按钮的单元格(我发现它们更容易放在集合视图单元格中),按钮的背景是一个特定的图像(现在硬编码),它带你到一个视图控制器,显示该项目的所有内容,没有什么特别的:)

但布局(设计)对我来说很难实现。基本上(根据我的理解)我需要有4个集合视图单元格,具有不同的定位按钮。现在我有一个集合视图控制器,带有一个集合视图,包含一个单元格。

#import "CollectionViewController.h"
#import "CollectionViewCell.h"
#import "ImageDetailsViewController.h"
#import "StoryItem.h"

@interface CollectionViewController ()
{
    NSArray *arrBigLeft;
    NSArray *arrSmallTopR;
    NSArray *arrSmallBotR;
}

@end

@implementation CollectionViewController

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

- (void)viewDidLoad
{


    arrBigLeft = [[NSArray alloc]initWithObjects:@"imageicon0@2x.jpg", @"imageicon1@2x.jpg", @"imageicon2@2x.jpg", @"imageicon3@2x.jpg", nil];

    arrSmallTopR = [[NSArray alloc]initWithObjects:@"imageicon4@2x.jpg", @"imageicon5@2x.jpg", @"imageicon6@2x.jpg", @"imageicon7@2x.jpg", nil];

    arrSmallBotR = [[NSArray alloc]initWithObjects:@"imageicon8@2x.jpg", @"imageicon9@2x.jpg", @"imageicon0@2x.jpg", @"imageicon1@2x.jpg", nil];

    [super viewDidLoad];

    // Do any additional setup after loading the view.
}

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

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

    if([segue.identifier isEqualToString:@"photoDetail1"]){
        CollectionViewCell *cell = (CollectionViewCell *)sender;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

        ImageDetailsViewController *idvc = (ImageDetailsViewController *)[segue destinationViewController];
        idvc.img = [UIImage imageNamed:[arrBigLeft objectAtIndex:indexPath.item]];

    }


    if([segue.identifier isEqualToString:@"photoDetail2"]){
        CollectionViewCell *cell = (CollectionViewCell *)sender;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];

        ImageDetailsViewController *idvc = (ImageDetailsViewController *)[segue destinationViewController];
        idvc.img = [UIImage imageNamed:[arrSmallTopR objectAtIndex:indexPath.item]];

    }


    if([segue.identifier isEqualToString:@"photoDetail3"]){
        CollectionViewCell *cell = (CollectionViewCell *)sender;
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
                ImageDetailsViewController *idvc = (ImageDetailsViewController *)[segue destinationViewController];
        idvc.img = [UIImage imageNamed:[arrSmallBotR objectAtIndex:indexPath.item]];

    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [arrBigLeft count];
}



// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *identifier=@"Cell";

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

    [[cell button1]setBackgroundImage:[UIImage imageNamed:[arrBigLeft objectAtIndex:indexPath.item]] forState:UIControlStateNormal];

    [[cell button2]setBackgroundImage:[UIImage imageNamed:[arrSmallTopR objectAtIndex:indexPath.item]] forState:UIControlStateNormal];

    [[cell button3]setBackgroundImage:[UIImage imageNamed:[arrSmallBotR objectAtIndex:indexPath.item]] forState:UIControlStateNormal];

    return cell;
}


-(IBAction)returned:(UIStoryboardSegue *)segue {

}



@end

这不是我想要的,但至少我得到了动态创建的单元格。我希望能够返回多个单元格,具有不同的项目数组(图像),并且它们都应该同时加载

screenshoot

这里你只能看到两个单元格,但我添加的第二个单元格只是为了想象我要解释的内容(4个不同的单元格) 当我运行应用程序时,第一个单元格(因为图像数量)重复4次,如果我要将相同的图像添加到第二个单元格,我希望在屏幕截图中看到它们,并继续按顺序重复等于图像数量(数组长度)

如果您需要更多代码,请与我们联系。谢谢!

1 个答案:

答案 0 :(得分:2)

我建议您按照这一点来基本了解所需的内容。

http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12

您可以进入更复杂的设计。看起来你跳进了深渊。但是本教程非常好。