[UICollectionViewCell imageView]: unrecognized selector sent to instance - objective-c

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.

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.

Add UIViewcontroller with xib with UITableView in UIScrollView

I'm trying to add a UIViewController in a UIScrollView, the UIViewController have a UITableView added with Interface Builder, i have connected it to data source and delegate to the File Owner's then i have connect the IBOutlet property with the ActivityView class, when i have do this to add to the UIScrollView:
ActivityView *viewActivity = [[ActivityView alloc] initWithNibName:#"ActivityView" bundle:nil];
[self.scrollView addSubview:viewActivity.view];
but when i open the view the app crash sometime with a BAD_EXC_ACCESS, sometime with a SIGBRT with this error:
[UILabel tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd27f8d0
and sometime is this:
[SMPageControl tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd68dcb0
and sometime is this:
[NSPathStore2 tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0xd3746b0
i can't understand how ti fix it, if i use the ActivityView alone, without adding it to the UIScrollview, works perfectly, and if i add ActivityView without the connection with the UITableView works, so i think the problem is adding it to the UIScrollView with the UITableView outlet connection, how i can do it?
this is the code of ActivityView:
#import "ActivityView.h"
#interface ActivityView () <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (strong, nonatomic) NSMutableArray *activities;
#end
#implementation ActivityView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.activities count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [self.activities objectAtIndex:indexPath.row];
return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.activities = [NSMutableArray array];
[self.activities addObject:#"row"];
}
EDIT: i have tried also to add the UITableView programmatically, without interface builder, and when i set the datasource delegate, crash...everyone can try...crash always...

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 TableView-Cell (Data from server) select multiple values with accessory change

I am working with TableView and wanted Custom TableViewCell to have a Small Image, Name and one custom image (With Tickmark and without Tick) can be on accessory to show if Cell is selected and if it's not selected it will show without Tick image on unselected cells.
And if i want to select multiple cells then it should show Tick image on selected Cells and Untick image on unselected cells and after that when i click on a button then i should be able to get the selected cell id's.
On the tableView i am getting all the values from the server and images also from URL's but the Tickmark and Unselected Tick mark image will be used the project itself.
So far i have created :
Class .h,.m,.xib of "ResultTableCell" of type UITableViewCell and my Main view "Result" with the TableView and a Button on top (on click of button i'll get the values of selected cells)
ResultTableCell.h
#import <UIKit/UIKit.h>
#interface ResultTableCell : UITableViewCell
#property (nonatomic, retain) IBOutlet UILabel *nameLabel;
#property (nonatomic, retain) IBOutlet UIImageView *thumbImageView;
ResultTableCell.m
#import "ResultTableCell.h"
#implementation ResultTableCell
#synthesize nameLabel,thumbImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
ResultTableCell.xib
The right hand side image on the xib is the place where the accessor image will come.
ResultTableCell.xib
And the main xib
Result.h
#import <UIKit/UIKit.h>
#import "ASIFormDataRequest.h"
#interface Results : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *nameData;
}
#property (nonatomic, retain)NSMutableArray *nameData;
#property (nonatomic, retain)NSMutableArray *ImageData;
#property (nonatomic, retain)NSMutableArray *idData;
#property (nonatomic, retain)UITableView *table;
#property (nonatomic, retain) IBOutlet UIButton *done;
#property (nonatomic, retain) NSMutableArray *arFors;
-(IBAction)save_done:(id)sender;
Result.m
#import "Results.h"
#import "ResultTableCell.h"
#interface Results ()
#end
#implementation Results
#synthesize arFors;
#synthesize done,nameData,table,addressData,ImageData,idData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.arFors=[NSMutableArray array];
// I am Getting Name,id and image url data from my HomeViewController
NSLog(#"Name Data from home view is %#",nameData); // 10 Names get's printed in log
NSLog(#"id Data is %#",idData);
NSLog(#"URL image data is %#",ImageData);
table = [[UITableView alloc]initWithFrame:CGRectMake(0, 221, 320, 327) style:UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Name data count is %d",nameData.count);
return nameData.count;
//return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/* UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}*/
static NSString *simpleTableIdentifier = #"ResultTableCell";
ResultTableCell *cell = (ResultTableCell *)[table dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ResultTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"table_tick" ]];
}
else {
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"table_add" ]];
}
NSLog(#"data is ************* %#",nameData);
cell.nameLabel.text = [nameData objectAtIndex:indexPath.row];
NSURL * imageURL = [NSURL URLWithString:[ImageData objectAtIndex:indexPath.row]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image2 = [UIImage imageWithData:imageData];
cell.ImageView.image = image2;
cell.ImageView.contentMode = UIViewContentModeScaleAspectFit;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"didSelectRowAtIndexPath %d",indexPath.row);
if ([self.arFors containsObject:[NSNumber numberWithInt:indexPath.row]]) {
[self.arFors removeObject:[NSNumber numberWithInt:indexPath.row]];
}
else{
[self.arFors addObject:[NSNumber numberWithInt:indexPath.row]];
// [self.table cellForRowAtIndexPath:indexPath];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[tableView reloadData];
}
-(IBAction)save_done:(id)sender
{
NSLog(#"selected cell values are %#",self.arFors);
}
Now with this code everything is working fine (tick image is shown on selected cells and untick image on unslected cells and on clicking the Done button i am getting the selected cell values),
But the Problem comes when i tapp on a cell then it like hangs and takes 5-6 seconds of time to change accessor image as it fires [tableView reloadData] in didselectrowatindexpath method so all data reloads again in the tableview and then the accessor image changes, please can any one correct my code or enhance it so that it works fast.
I have tried a lot of ways but i was not able to do it without the reloading of table and if i reload table it takes long time.
Coding help will be much tankful.
Your problem is:
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
Because it downloads all of the image data from the network each time you reload the table view. You should be doing this asynchronously and caching the returned image so you don't need to download it repeatedly. Take a look at a library like SDWebImage to help you with this.