Pregunta

I am making social app and I have place there I need to make 6 images on page. In this place usually I gave text or one image. How can I set multi images in that place now?

I want set frame for this place and then I want that images scaled to my frame. Is it possible and that element I need to use here? UICOllections or that?

¿Fue útil?

Solución

What I understood from your question you'll need to use UICollectionView. Your should follow these steps:

  1. Make an NSArray of all your images. Ex. NSArray *imageArray=[[NSArray alloc]initWithObjects:@"image1.png",@"image2.png",@"image3.png", nil];

  2. Drag a UICollectionView to your storyboard, create a custom cell, and use a UIImageView on that custom cell.

  3. Assign a new class to your custom cell.

  4. Connect an outlet for image view in your custom cell's class.

  5. Connect delegate and datasource of your UICollectionView.

  6. Use the following delegate methods for UICollectionView:

    -(NSInteger)numberOfSectionsInCollectionView:
    (UICollectionView *)collectionView
    {
    return 1;
    }
    
    -(NSInteger)collectionView:(UICollectionView *)collectionView
        numberOfItemsInSection:(NSInteger)section
    {
    return imageArray.count;
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
    CustomCellClass *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
    
    UIImage *image;
    int row = [indexPath row];
    image = [UIImage imageNamed:imageArray[row]];
    myCell.imageview.image = image;
    
    return myCell;
    }
    

Hope this helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top