【发布时间】:2013-05-19 17:29:51
【问题描述】:
我已经实现了一个自定义表格单元格,并且当表格进入 cellForRowAtIndexPath 时收到运行时错误(“无法识别的选择器发送到实例”)。尝试实例化自定义单元格时发生错误。我之前已经成功实现了这一点,但现在错误不会消失。我有一个 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;
}
这是完整的错误信息:
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[__NSCFConstantString instantiateWithOwner:options:]:无法识别的选择器已发送到实例 0x24bb0'
谁能提供一些关于检查什么的建议?谢谢!薇薇安
【问题讨论】:
-
错误信息是什么(告诉我们实例和方法名称)。
-
您是否为自定义单元格使用 XIB?如果是这样,可能是
UITableView自动实例化UITableViewCell而不是您的自定义单元类。 (是的,我 -
在帖子中添加了错误信息
-
不,我没有为自定义单元格使用 xib。我刚刚创建了 UITableViewCell 的子类
-
看起来“站点名称”可能有问题。您可以在将其从阵列中取出后打印出来吗?
标签: ios objective-c uitableview