“无法识别的选择器发送到实例"使用自定义表格单元格时 [英] "Unrecognized selector sent to instance" when using custom table cell

查看:26
本文介绍了“无法识别的选择器发送到实例"使用自定义表格单元格时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个自定义表格单元格,当表格进入 cellForRowAtIndexPath 时,我收到了一个运行时错误(无法识别的选择器发送到实例").尝试实例化自定义单元格时发生错误.我以前成功地做到了这一点,但现在错误不会消失.我有一个原型单元格,它的自定义类属性设置为自定义单元格 UITableViewCell 子类.这是自定义单元格:

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:

* 由于未捕获的异常NSInvalidArgumentException"而终止应用程序,原因:-[__NSCFConstantString instantiateWithOwner:options:]: 无法识别的选择器发送到实例 0x24bb0'

* 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 是一个好主意如果您在单独的 NIB 文件中定义了自定义单元格.问题是您传递的是 NSString 而不是 UINIb 对象.

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.

改为这样做:

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

但是因为您使用的是原型单元而不是单独的 NIB,所以根本没有必要这样做.相反,您只需要确保正确设置原型单元格的重用标识符(在本例中为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").

这篇关于“无法识别的选择器发送到实例"使用自定义表格单元格时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆