如何将照片添加到UICollectionViewCell?

时间:2014-03-25 07:23:36

标签: ios objective-c uicollectionview uicollectionviewcell

我创建了一个UICollectionView,我目前已经显示了白色单元格。我一直在关注这个tutorial以找出如何使用委托方法等。

我已经达到了创建创建自定义UICollectionViewCells 的程度,但在本例中,他告诉您打开故事板等教程。我没有故事板。我试图按照说明创建视图,但是使用我自己的.xib文件,我无法使用任何东西。

如何查看我的委托方法目前在哪里?

// this is how I load the collection view
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
    photoCollectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(10.0, 40.0, 480.0, 713.0) collectionViewLayout:layout];

    photoCollectionView.dataSource = self;
    photoCollectionView.delegate = self;
    [self.view addSubview:photoCollectionView];

    [photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]];
    [photoCollectionView.layer setBorderWidth: 1.0];
    [self.photoCollectionView reloadData];



// this is what my delegate methods look like
#pragma mark - CollectionView Delegates
#pragma mark -- UICollectionView Datasource
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
    return [imageArray count];
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
    return 1;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];

    // I think this is where I want to set my image for the cell
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];

    // but I have no idea where to pass the imag....

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


#pragma mark -- UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // TODO: Select Item
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    // TODO: Deselect item
}


#pragma mark –- UICollectionViewDelegate FlowLayout

// 1
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {   
    CGSize retval = CGSizeMake(210, 157+20); // add 20 for labels under neath.. might need to adjust this.
    return retval;
}


- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(50, 20, 50, 20);


}Subview:photoCollectionView];

        [photoCollectionView.layer setBorderColor: [[UIColor lightGrayColor] CGColor]];
        [photoCollectionView.layer setBorderWidth: 1.0];
        [self.photoCollectionView reloadData];

正如您在 collectionView:cellForItemAtIndexPath 中看到的那样,我准备好了我的图像

NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
        UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];

我只需要知道如何将它加载到collectionviewcell中。

2 个答案:

答案 0 :(得分:0)

请尝试像这样使用....

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

    // I think this is where I want to set my image for the cell
    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];
[cell setBackgroundColor:[UIColor colorWithPatternImage:imageForCollection];// Add your image here........

    // but I have no idea where to pass the imag....

    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

我希望它会帮助你...

答案 1 :(得分:0)

我有名为PhotoCell的UICollectionViewCell。并将label和imageview作为属性。 这就是你可以将Image设置为单元格的方法。愿这是你的要求。我不知道。试试这个

您需要将UICollectionViewCell的Xib上的标识符设置为MY_CELL或其他名称。然后

的.m

 static NSString *cellidentity=@"MY_CELL";

在ViewDidLoad方法

UINib *nib;
nib = [UINib nibWithNibName:@"PhotoCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:cellidentity];

我的UICollectionViewCell .h是

@interface PhotoCell : UICollectionViewCell

@property (retain, nonatomic) IBOutlet UILabel* label;

@property (retain, nonatomic) IBOutlet UILabel* timelineLabel;

@property (retain, nonatomic) IBOutlet UIImageView* yourImageView;

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
   // PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"PhotoCell" //forIndexPath:indexPath];
PhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellidentity forIndexPath:indexPath];

    NSDictionary *currentPhotoDict = [imageArray objectAtIndex:indexPath.row];
    UIImage *imageForCollection = [UIImage imageWithData:[currentPhotoDict objectForKey:@"DImage"]];

    // but I have no idea where to pass the imag....

    cell.label.text = [NSString stringWithFormat:@"%d",indexPath.item];

    cell.yourImageView.image=imageForCollection;

    return cell;
}

希望它可以帮助你......