Setting cell in UICollectionView - objective-c

I am attempting to set an imageView in a cell in a UICollectionview. No images ever appear. I am also tried to set a label but have had no luck
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
photoCollectionViewCell *photoCell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
NSURL *url = [NSURL URLWithString:self.imageURLArray[indexPath.row]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
//photoCell.photoImageView.image = tmpImage;
photoCell.photoLabel.text = #"l";
return photoCell;
}
One thing I did notice is that "photoCell" always returns null.
I am registering my collectionview like this:
[self.collectionView registerClass:[photoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
My cell interface header file is:
#interface photoCollectionViewCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *photoImageView;
#property (weak, nonatomic) IBOutlet UILabel *photoLabel;
#end

My first guess would be to return an instance of photoCollectionViewCell instead of the default UICollectionViewCell:
- (photoCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
}
Second guess:
Where do you set your "reuseIdentifier" variable and what value is assigned to it? You can set a reuseIdentifier as follows:
static NSString *CellIdentifier = #"MyCustomCell";
and then set it to your:
[self.collectionView registerClass:[photoCollectionViewCell class] CellIdentifier];
and:
photoCollectionViewCell *photoCell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
and register your ReuseIdentifier on your storyboard custom cell:

Related

UICollection View - Dont adds new custom cells

I have a collectionView inside a view. I did setup hte datasource, the delegate and everything.
This cell is working:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [self.collectionAddedMembers dequeueReusableCellWithReuseIdentifier:cellReuseIdentifierAdded forIndexPath:indexPath];
cell.backgroundColor = [colores objectAtIndex:arc4random_uniform(6)];
return cell;
}
But on my custom cell AddedMemberCollectionViewCell it dont adds new cells:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
AddedMemberCollectionViewCell *cell = [self.collectionAddedMembers dequeueReusableCellWithReuseIdentifier:cellReuseIdentifierAdded forIndexPath:indexPath];
NSArray *firstSection = [selectedMembersData objectAtIndex:0];
SelectedContacto *cellData = [firstSection objectAtIndex:indexPath.row];
NSMutableDictionary *dataForConfigure = [[NSMutableDictionary alloc] init];
[dataForConfigure setObject:cellData.contactAvatar forKey:#"contactAvatar"];
[dataForConfigure setObject:cellData.contactName forKey:#"contactName"];
[dataForConfigure setObject:cellData.memberId forKey:#"contactName"];
[cell configure:dataForConfigure];
return cell;
}
This is the code of the custom cell.
.h file:
#interface AddedMemberCollectionViewCell : UICollectionViewCell
#property (nonatomic) IBOutlet UIImageView *avatar;
#property (nonatomic) IBOutlet UILabel *memberName;
#property (nonatomic) IBOutlet UIButton *removeMember;
#property (nonatomic) NSNumber *memberId;
- (void) configure:(NSDictionary*)cellData;
#end
.m file
#import "AddedMemberCollectionViewCell.h"
#implementation AddedMemberCollectionViewCell
- (void) configure:(NSDictionary*)cellData {
self.avatar = [cellData objectForKey:#"contactAvatar"];
self.memberName = [cellData objectForKey:#"contactName"];
self.memberId = [cellData objectForKey:#"memberId"];
}
#end
I could provide more code, but I am thinking would not be necessary. What is happening here?
EDIT:
Registering in viewDidLoad:
self.searchBar.delegate = self;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.collectionAddedMembers.dataSource = self;
self.collectionAddedMembers.delegate = self;
[self.collectionAddedMembers registerClass:AddedMemberCollectionViewCell.class forCellWithReuseIdentifier:cellReuseIdentifierAdded];
Are you sure that the cell is not actually added to your collection view?
However, you were not doing it right, as calling dequeueReusableCellWithReuseIdentifier will NOT create UICollectionViewCell but re-use an existing cell, hence your initialization code in init will never have chance to be called.
A good approach should look like this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *cellData = [data objectAtIndex:indexPath.row];
// Call custom cell's configure function to init cell content
[cell configure:cellData];
return cell;
}
#interface CustomCell : UICollectionViewCell
-(void)configure:(NSDictionary *)data;
#end
#implementation CustomCell
-(void)configure:(NSDictionary *)data {
// Init your cell content
}
#end

CollectionViewController not seeing properties on the DetailViewController

I'm a novice and I'm trying to understand why my CollectionViewController is not seeing properties on the DetailViewController...any tips or insight would be appreciated. Code below...for my detailViewController.h and also my CollectionViewController.m, respectively.
// DetailViewController.h
// RecipePhoto
//
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIImageView *recipeImageView;
#property (nonatomic) NSString *recipeImageName;
- (IBAction)close:(id)sender;
#end
#import "RecipeCollectionViewController.h"
#import "DetailViewController.h".
#interface RecipeCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
{
NSArray *recipeImages;
}
#end
#implementation RecipeCollectionViewController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
recipeImages = [NSArray arrayWithObjects:#"angry_birds_cake.jpg", #"creme_brelee.jpg", #"egg_benedict.jpg", #"full_breakfast.jpg", #"green_tea.jpg", #"ham_and_cheese_panini.jpg", #"ham_and_egg_sandwich.jpg", #"hamburger.jpg", #"instant_noodle_with_egg.jpg", #"japanese_noodle_with_pork.jpg", #"mushroom_risotto.jpg", #"noodle_with_bbq_pork.jpg", #"starbucks_coffee.jpg", #"thai_shrimp_cake.jpg", #"vegetable_curry.jpg", #"white_chocolate_donut.jpg", nil];
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipePhoto"]) {
NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
// Get the new view controller using [segue destinationViewController].
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
// Pass the selected object to the new view controller.
DetailViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
[self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//return arrayName.count;
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return recipeImages.count;
}
//provides the data for the collection view cells
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
// Configure the cell
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[recipeImages objectAtIndex:indexPath.row]];
return cell;
}
You are trying to use recipeImageName as a class property in prepareForSegue:
DetailViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
should be
detailViewController.recipeImageName = [recipeImages[indexPath.section] objectAtIndex:indexPath.row];
Pay close attention to the capitalisation of the first letter. DetailViewController is the class; detailViewController is the local variable.

[UICollectionViewCell imageView]: unrecognized selector sent to instance

I am trying to use collection view and display static images in that, but I get the following error:
[UICollectionViewCell imageView]: unrecognized selector sent to instance.
I have configured cell identifire = Cell.
Here is my code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
SSCollectionViewCell *cell = (SSCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
int imageNumber = indexPath.row % 10;
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"Image%d.jpg",imageNumber]];
return cell;
}
here is the sscollectionViewCell.h code
#interface SSCollectionViewCell : UICollectionViewCell
#property (weak) IBOutlet UIImageView *imageView;
#end
here is the SSColletionViewCell.m code
#import "SSCollectionViewCell.h"
#interface SSCollectionViewCell ()
#end
#implementation SSCollectionViewCell
#synthesize imageView;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
#end
Can any one suggest where i made mistake.?
Change
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
To
[self.collectionView registerClass:[SSCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
You missed this in the ViewDidLoad Method
[self.collectionView registerClass:[SSCollectionViewCell class] forCellWithReuseIdentifier:#"Cell"];
Your cell is still a UICollectionCell and not your custom SSCollectionViewCell, It´s for that does´t know the message (unrecognized selector).
Look in stroryboard, be sure the select the collectionCell, and change it´s class (third tab in right menu), in CustomClass put your now.

UICollectionView not showing cells:

I am trying to use UICollectionView to show the first 10 photos from my facebook profile. However, whenever I run the app, the UICollectionView does not show anything - it remains black and I dont see any cells. (I have set the background colour of the cells to blue and colour of the CollectionView to black, but I dont even see the blue cells)
I am using SDWebImage to load photos onto the cells.
Thank you in advance for any help
Here is my code:
In my header file -
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
#interface LNViewController : UIViewController <FBFriendPickerDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
#end
In my .m file -
#import "LNViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#property (weak, nonatomic) IBOutlet UICollectionView *photoCollectionView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self refreshArray];
//fetch photos from facebook (this function also has [self.photoCollectionView reloadData] in it;
[self fetchPhotosMe];
[self.photoCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"photoCell"];
[self.photoCollectionView reloadData];
}
#pragma Mark UICollectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"photoCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//create array with URLs
NSMutableArray *photosURL = [[NSMutableArray alloc] init];
for (int i = 0; i <10; i++){
NSDictionary *photoDic = [self.photosMeArray objectAtIndex:i];
[photosURL addObject:[photoDic valueForKey:#"src"]];
}
//Load Cells
UIImageView *photoImageView = (UIImageView *)[cell viewWithTag:100];
[photoImageView setImageWithURL:[NSURL URLWithString:[photosURL objectAtIndex:indexPath.row]]];
return cell;
}

Custom UITableView not refreshing cells

I am creating a custom UITableViewCells. I am using a NIB file as the cell. Its displaying some data from REST API's. The problem I am having is when I scroll down & then back up, the cells are not refreshed. It shows the data from when I scrolled down. Here's my code -
MyViewController.h
#interface FLOViewController : UIViewController <UISearchBarDelegate,
UITableViewDelegate,
UITableViewDataSource>
{
UISearchBar *sBar;
UITableView *searchResTable;
NSArray *searchRes;
UITableViewCell *resultsOne;
}
#property (nonatomic, retain) IBOutlet UILabel *photos;
#property(nonatomic, retain) IBOutlet UITableView *searchResTable;
#property (nonatomic, retain) NSArray *searchRes;
/* Search Results Templates */
#property (nonatomic, assign) IBOutlet UITableViewCell *resultsOne;
MyViewController.m
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.searchRes == nil)
return nil;
static NSString *cId = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cId];
if(cell == nil)
{
NSLog(#"CREATING NEW CELL");
[[NSBundle mainBundle] loadNibNamed:#"ResultsOne" owner:self options:nil];
cell = self.resultsOne;
self.resultsOne = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//from here on displaying images etc from REST API.
UIImageView *uPhoto = (UIImageView *)[cell viewWithTag:2];
NSString *photoURL = [[self.searchRes objectAtIndex:[indexPath row]] objectForKey:#"originator_photo_url"];
if(photoURL)
{
[UIView beginAnimations:#"fadeIn" context:NULL];
[UIView setAnimationDuration:0.5];
NSString *urlString = [NSString stringWithString:photoURL];
[uPhoto setImageWithURL:[NSURL URLWithString:urlString]
placeholderImage:[UIImage imageNamed:[NSString stringWithFormat:#"ph%d.png",[indexPath row]]]];
[UIView commitAnimations];
}
//similarly display other sections in the cell...
Why are the contents of my cell not refreshing? Even when I put in completely new data (through search from REST API's) some of the cells still show old views in the tablecells.
UPDATE: If I comment out UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cId]; then the problem is solved. i.e. I dont see repetition of cell content on scrolling.
Because I am creating custom ImageViews in UITableViewCell do i need to do something special to clear out the cell content before new content can be added??
To me it looks like you forgot to reset the uPhoto-UIImageView, in case the photoURL is nil. This brings in cached data.
if (photoURL) {
...
}
else {
uPhoto.image = nil;
}
You are using
static NSString *cId = #"Cell";
Let's try different identifier for each cell. I face this problem and solve it by using different cell identifier like
static NSString *cId =[NSString StringWithFormat : #"Cell_%d_%d",[indexPath section], [indexPath row] ];
I think it may solve your problem.