"Unrecognized selector sent to instance" when using a custom table cell

I have implemented a custom table cell and get a runtime error ("Unrecognized selector sent to instance") when the table enters cellForRowAtIndexPath. An error occurs when trying to create an instance of a custom cell. I have achieved this successfully before, but now the error will not go away. I have a prtotype cell, and its own class attribute is set to a custom element of a subclass of UITableViewCell. Here is the user cell:

#import "FavoriteCell.h" @implementation FavoriteCell @synthesize lblGaugeID, lblMainTitle, bgImage; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { bgImage = [UIImage imageNamed:@"tableCellBG.png"]; self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]; UIColor *foregroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0]; //UIColor *shadowColot = [UIColor colorWithWhite:0.75 alpha:1.0]; CGSize size = self.contentView.frame.size; self.backgroundView = [[UIImageView alloc] initWithImage:bgImage]; self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, 0.5, size.width-16, size.height-40)]; [self.lblMainTitle setFont:[UIFont systemFontOfSize:12.0]]; [self.lblMainTitle setTextAlignment:NSTextAlignmentLeft]; [self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; [self.lblMainTitle setBackgroundColor:transparentBG]; self.lblGaugeID = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 31.0, size.width-16, size.height-40)]; [self.lblGaugeID setFont:[UIFont boldSystemFontOfSize:15.0]]; [self.lblGaugeID setTextAlignment:NSTextAlignmentLeft]; [self.lblGaugeID setTextColor:foregroundColor]; [self.lblGaugeID setShadowOffset:CGSizeMake(0.2 , 0.2)]; [self.lblGaugeID setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; [self.lblGaugeID setBackgroundColor:transparentBG]; [self.contentView addSubview:lblGaugeID]; [self.contentView addSubview:lblMainTitle]; } return self; } @end 

and here it is initialized (issued from my view controller class):

 -(FavoriteCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellIdentifier = @"favoriteCell"; FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; //error if(cell == nil){ cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; } NSString *siteName = [faveKeys objectAtIndex:indexPath.row]; [cell.lblMainTitle setText:siteName]; [cell.lblGaugeID setText:[allFavorites objectForKey:siteName]]; return cell; } 

Here is the complete error message:

* Application termination due to the uncaught exception "NSInvalidArgumentException", reason: '- [__ NSCFConstantString instantiateWithOwner: options:]: unrecognized selector sent to instance 0x24bb0'

Can anyone advise what to check? Thank you Vivian

+6
source share
1 answer

Using - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier was a good idea if you defined your custom cell in a separate NIB file . The problem was that you passed an NSString, not a UINib object.

Do this instead:

[self.tblFavorites registerNib:[UINib nibWithNibName:@"favoriteCellView" bundle:nil] forCellReuseIdentifier:@"favoriteCell"];

However , since you used a prototype cell and not a separate NIB, there is no need to do this at all. Instead, you just need to make sure that the prototype identifier reuse identifier is set correctly (in this case, “favoriteCell”).

+11
source

Source: https://habr.com/ru/post/945372/