1

我才刚接触 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

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

截图

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

如果您需要更多代码,请告诉我.. 谢谢!

4

1 回答 1

2

我建议遵循此内容以基本了解所需内容。

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

比你可以推进更复杂的设计。看起来你跳进了深渊。但是这个教程真的很好。

于 2013-09-14T12:39:25.333 回答