使用自定义表格单元格时,“无法识别的select器发送到实例”

我已经实现了一个自定义表格单元格,并且在表格inputcellForRowAtIndexPath时收到一个运行时错误(“无法识别的select器发送给实例”)。 尝试实例化自定义单元格时发生错误。 我以前成功地做到了这一点,但现在错误不会消失。 我有一个prtotype单元格,并将其自定义类属性设置为自定义单元格UITableViewCell子类。 这是自定义单元格:

#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 

这里是它的安装位置(摘自我的视图控制器类):

 -(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; } 

以下是完整的错误消息:

*由于未捕获exception“NSInvalidArgumentException”而终止应用程序,原因:' – [__ NSCFConstantString instantiateWithOwner:options:]:无法识别的select器发送到实例0x24bb0'

任何人都可以提供一些build议,以检查什么? 谢谢! 维维安

使用- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier是一个好主意,如果你已经定义你的自定义单元格在一个单独的NIB文件 。 问题是你传递了一个NSString而不是一个UINib对象。

做这个,而不是:

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

但是由于您使用的是原型单元而不是单独的NIB,因此完全不需要这样做。 相反,您只需确保原型单元的重用标识符已正确设置(在本例中为“favoriteCell”)。