xcode iOS 6 Collection View Controller 多个动态单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18769244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS 6 Collection View Controller multiple dynamic cells
提问by Mite Stojanov
I am only a week into Xcode, trying to learn something, so I can contribute to a project..
我才刚接触 Xcode 一周,试图学习一些东西,这样我就可以为一个项目做出贡献..
So far I've been looking into tutorials and made something :) But I'm far from over. I hope someone will understand what I'll be asking..
到目前为止,我一直在研究教程并制作了一些东西:) 但我还远远没有结束。我希望有人会明白我会问什么..
I'm making an app that will take data from a server, the data will include thumbnail images, images and text. Right now I have a collection view controller, with a cell that has 3 buttons (I find them easier to place around in the collection view cell), the button's background is a specific image (hard coded for now), and it takes you to a view controller, where all the content for that item are shown, nothing special :)
我正在制作一个从服务器获取数据的应用程序,数据将包括缩略图、图像和文本。现在我有一个集合视图控制器,其中一个单元格有 3 个按钮(我发现它们更容易放在集合视图单元格中),按钮的背景是一个特定的图像(现在是硬编码的),它会带你到一个视图控制器,其中显示了该项目的所有内容,没什么特别的:)
But the layout (design) is hard for me to achieve. Basically (from my understanding) I need to have 4 collection view cells, with different positioned buttons. Now I have one collection view controller, with one collection view , containing one cell.
但是布局(设计)对我来说很难实现。基本上(根据我的理解)我需要有 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:@"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", nil];
arrSmallTopR = [[NSArray alloc]initWithObjects:@"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", nil];
arrSmallBotR = [[NSArray alloc]initWithObjects:@"[email protected]", @"[email protected]", @"[email protected]", @"[email protected]", 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
This doesn't work as I want it to yet, but at least I'm getting dynamically created cells. I'd like to be able to return multiple cells, with different arrays of items (images), and they all should load at the same time
这还不能如我所愿,但至少我得到了动态创建的单元格。我希望能够返回具有不同项目(图像)数组的多个单元格,并且它们都应该同时加载
here you can see only two cells, but the second one I added just to visualize what I'm trying to explain (4 different cells) The first cell (because of the number of images) repeats 4 times when I ran the app, if I was about to add the same images to the second cell, I would like to see them as in the screen shoot, and to continue to repeat in that order equal to the number of images (length of array)
在这里您只能看到两个单元格,但我添加的第二个单元格只是为了可视化我要解释的内容(4 个不同的单元格) 第一个单元格(由于图像数量)在我运行应用程序时重复 4 次,如果我正准备将相同的图像添加到第二个单元格中,我希望在屏幕拍摄中看到它们,并继续按等于图像数量(数组长度)的顺序重复
If you need more code, let me know.. thanks!
如果您需要更多代码,请告诉我..谢谢!
回答by DogCoffee
I suggest following this to get a basic understanding of whats required.
我建议按照这个来获得对所需内容的基本了解。
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
Than you can advance to more complex designs. Looks like your jumping into the deep end. But this tutorial is really good.
比您可以推进到更复杂的设计。看起来你跳入了深渊。但是这个教程真的很好。