0

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.

Community
  • 1
  • 1
user3239274
  • 29
  • 1
  • 1
  • 5
  • 1
    Go through basics http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12 – preetam Feb 17 '14 at 06:59
  • 3
    As a beginner you should follow complete tutorials on web instead of SO and then come here for your queries. – preetam Feb 17 '14 at 07:01

3 Answers3

0

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 :)

ingconti
  • 9,213
  • 2
  • 51
  • 39
-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:

enter image description here

codercat
  • 21,439
  • 9
  • 56
  • 84
  • what you have problem in your project – codercat Feb 17 '14 at 07:51
  • Yes i have placed your function by placing images and declaring the similar array just like in tutorial...but it is not showing images in cell ...i think their are certain command needed for connection with imageviewer and collection-cell with file owner ...just like stuff we do manually in storyboard.... – user3239274 Feb 17 '14 at 08:39
  • if possible link your project – codercat Feb 17 '14 at 09:13
  • you can download project from this link: https://skydrive.live.com/redir?resid=6D87C4A87C9F0A2F!141&authkey=!AJR2hw3GrECHHfs&ithint=file%2c.zip – user3239274 Feb 17 '14 at 13:12
  • Because you're calling `[cell viewWithTag:100]`, I assume that your view is added as a subview to the cell. This is incorrect. One should not add subviews to the cell directly, but rather to the cell's contentView. i.e. `[cell.contentView addSubview:imageView];` & `[cell.contentView viewWithTag:100];` – Logan May 15 '14 at 19:01
-1

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.

Lithu T.V
  • 19,491
  • 11
  • 53
  • 98
ingconti
  • 9,213
  • 2
  • 51
  • 39
  • 3
    I think it would be better to show the correct answer rather than to restate the incorrect code. I think they want `didSelectCellAtIndex`. – Logan May 15 '14 at 19:25