I have implemented a custom table cell and am receiveing a runtime error ("Unrecognized selector sent to instance") when the table enters cellForRowAtIndexPath. The error occur when trying to instantiate the custom cell. I have achieved this successfully before, but now the error won't go away. I have a prtotype cell and its custom class attribute is set to the custom cell UITableViewCell subclass. Here is the custom 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 is where it is instatiated (excerpted 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 full error message:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString instantiateWithOwner:options:]: unrecognized selector sent to instance 0x24bb0'
Can anyone offer some advice on what to check? Thanks! Vivian
Using - (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier
was a good idea if you had defined your custom cell in a separate NIB file. The problem was that you passed an NSString rather than a UINib object.
Do this instead:
[self.tblFavorites registerNib:[UINib nibWithNibName:@"favoriteCellView" bundle:nil] forCellReuseIdentifier:@"favoriteCell"];
However since you used a prototype cell rather than a separate NIB, there's no need to do this at all. Instead you just need to make sure the prototype cell's reuse identifier is set properly (in this case to "favoriteCell").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With