Home:ALL Converter>How to insert images in uicollectionview programmatically?

How to insert images in uicollectionview programmatically?

Ask Time:2014-02-17T14:55:55         Author:user3239274

Json Formatter

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.

Author:user3239274,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21822616/how-to-insert-images-in-uicollectionview-programmatically
ingconti :

as Logan suggest:\n\n#define IMG_TAG 1000\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n static NSString *CellIdentifier = @\"MyCell\";\n UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];\n\n // Configure the cell...\n cell.textLabel.text = @\"test.....\";\n UIImage * img = [UIImage imageNamed: @\"sampleImage\"];\n\n UIImageView * customImageView = (UIImageView*) [cell viewWithTag: IMG_TAG];\n // two cases:\n // 1) we got a \"recycled\" cell, so we already have an image view added\n // 2) we got a \"new\" cell, so we must create, add image view AND TAg\n // in both cases we will set image\n\n if (customImageView){\n // case 1\n // nothing special\n }else{\n // case 2:\n // add and tag:\n customImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 2, 30, 30)];\n [cell addSubview: customImageView];\n customImageView.tag = IMG_TAG;\n }\n\n customImageView.image = img;\n return cell;\n}\n\n\npls review and upvote :)",
2016-10-02T09:27:13
codercat :

Beginner read tutroial and understand first everyting in below link and apple doc\n\nios-programming-uicollectionview-tutorial-with-sample-code\n\nhttps://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html\n\nchange this your blackground color like this approach\n\n- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath\n{\n UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@\"cellIdentifier\" forIndexPath:indexPath];\n UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];\n recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];\n cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@\"photo-frame.png\"]];\n [self.view addSubview:recipeImageView];\n cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]]];\n\n return cell;\n}\n\n\noutput:\n\n",
2014-02-17T07:08:55
yy