UICollectionView仅显示第一个项目

时间:2013-11-16 12:40:32

标签: ios objective-c uicollectionview uicollectionviewcell

我正在开发一个类似于视频专辑的项目。我正在使用UICollectionView来显示这些视频的拇指图像。最糟糕的是我不应该使用storyboard或xib文件。我试图以编程方式执行此操作。我目前正在编写此代码:

- (void)viewDidLoad
{
    i = 0;

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    [layout setItemSize:CGSizeMake(self.view.frame.size.width/2.5,self.view.frame.size.width/2.5)];

    collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    [collectionView setDataSource:self];
    [collectionView setDelegate:self];
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];

    [self.view addSubview:collectionView];

    [super viewDidLoad];
}

我已在numberOfSectionsInCollectionView[myArray count]中返回1,以便numberOfItemsInSection返回。

-(UICollectionViewCell *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   UICollectionViewCell *cell = [cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];

    cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];

    imageView.image = [UIImage imageNamed:[storeData objectAtIndex:i]];

    [cell addSubview:imageView];

    i++;

    return cell;
}

我已经重新检查了myArray中的图片。加载视图时,集合视图仅显示第一个图像。其他4个单元格是空的。我的代码出了什么问题?

4 个答案:

答案 0 :(得分:3)

对于遇到此问题的人,frame是关键。我遇到了这个并改变了:

cell.imageView.frame = cell.frame;

cell.imageView.frame = cell.bounds;

操作正在使用:

cell.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3))];

这就是为什么会发生这种情况。

答案 1 :(得分:1)

您不应该使用i作为计数器。向您发送indexPath的委托方法的重点在于它告诉您从源数据数组中获取哪些信息。因此,请删除i并改为使用indexPath.row

您也不需要2张图片视图。但是你应该保留你的特殊子视图,而不是使用图像视图中内置的单元格。

答案 2 :(得分:0)

你不需要柜台。如Wain所示,使用indexPath.row

重要的是,您不应在cellForItemAtIndexPath中创建新的子视图,而应使用此方法根据内容适当填充它们。您可以将图像视图放入故事板原型单元格中,并使用标签识别它们。从dequeueReusableCellWithReuseIdentifier返回的每个单元格都已包含图像视图。

答案 3 :(得分:0)

你永远不应该在cellForRowAtIndexPath:中创建ui元素。像这样对集合视图单元格进行子类化:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        NSLog(@"INIT WITH FRAME FOR CELL");
        //we create the UIImageView here
        imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.frame = CGRectMake(cell.frame.origin.x , cell.frame.origin.y, cell.frame.size.width, (cell.frame.size.height - cell.frame.size.height/3));
        [self.contentView addSubview:imageView]; //the only place we want to do this addSubview: is here!
    }
    return self;
}

然后将该子类化单元格添加为属性并更改此代码:]

[collectionView registerClass:[customCellClass class] forCellWithReuseIdentifier:@"MyCell"];

执行这些更改:

-(customCellClass *) collectionView:(UICollectionView *)cV cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   UICollectionViewCell *cell = (customCellClass *)[cV dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor blackColor];

    imageView.image = [UIImage imageNamed:[storeData objectAtIndex:indexPath.row]];

    return cell;
}

最后的调整是将[super viewDidLoad]移到此处:

- (void)viewDidLoad
{
     [super viewDidLoad];
     //insert the rest of the code here rather than before  viewDidLoad
}