-->

How to insert images in uicollectionview programma

2020-08-01 06:18发布

问题:

I'm pretty new to Objective-C so hopefully this all makes sense..I ran code provided in first answer Creating a UICollectionView programmatically..It is working fine .Now i want to add some pictures in cell that can expanded by mouse click .I searched many tutorial but all using nib files or storyboard files .How i can accomplish this task programmatically ?

Any help would be greatly appreciated. Thanks in advance.

回答1:

Beginner read tutroial and understand first everyting in below link and apple doc

ios-programming-uicollectionview-tutorial-with-sample-code

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html

change this your blackground color like this approach

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    [self.view addSubview:recipeImageView];
    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];

    return cell;
}

output:



回答2:

as Logan suggest:

#define IMG_TAG 1000
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    cell.textLabel.text = @"test.....";
    UIImage * img = [UIImage imageNamed: @"sampleImage"];

    UIImageView * customImageView = (UIImageView*) [cell viewWithTag: IMG_TAG];
    // two cases:
    // 1) we got a "recycled" cell, so we already have an image view added
    // 2) we got a "new" cell, so we must create, add image view AND TAg
    // in both cases we will set image

    if (customImageView){
        // case 1
        // nothing special
    }else{
        // case 2:
        // add and tag:
        customImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 2, 30, 30)];
        [cell addSubview: customImageView];
        customImageView.tag = IMG_TAG;
    }

    customImageView.image = img;
    return cell;
}

pls review and upvote :)



回答3:

NOTE:

code above is WRONG!

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];
    [self.view addSubview:recipeImageView];
    cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];

    return cell;
}

You allocate (better to allocate ONCE unsung TAGs...) but the code will ADD subview EVERY time cellForItemAtIndexPath is called.