【问题标题】:iOS 6 Collection View Controller multiple dynamic cellsiOS 6 Collection View Controller 多个动态单元格
【发布时间】:2013-09-12 16:06:20
【问题描述】:

我刚接触 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 次,如果我要向第二个单元格添加相同的图像,我希望在屏幕截图中看到它们,并继续以等于图像数量(数组长度)的顺序重复

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

【问题讨论】:

    标签: iphone xcode uicollectionviewcell collectionview


    【解决方案1】:

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

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

    您可以进行更复杂的设计。看起来你跳进了深渊。不过这个教程真的很好。

    【讨论】:

    • 感谢您的教程,我可以看到您可以使用 collectionView 做很多事情。我会好好检查一下 :)
    • 是的,寻找您可以做的很酷的事情。如果您对答案感到满意,请标记为正确。
    猜你喜欢
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多