gpt4 book ai didi

ios - "Unrecognized selector sent to instance"使用自定义表格单元格时

转载 作者:可可西里 更新时间:2023-11-01 03:40:02 29 4
gpt4 key购买 nike

我已经实现了自定义表格单元格,当表格进入 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

这是实例化的地方(摘 self 的 View Controller 类):

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

这是完整的错误信息:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString instantiateWithOwner:options:]: unrecognized selector sent to instance 0x24bb0'

任何人都可以就检查的内容提供一些建议吗?谢谢!薇薇安

最佳答案

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

改为这样做:

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

但是因为您使用的是原型(prototype)单元而不是单独的 NIB,所以根本没有必要这样做。相反,您只需要确保正确设置原型(prototype)单元格的重用标识符(在本例中为“favoriteCell”)。

关于ios - "Unrecognized selector sent to instance"使用自定义表格单元格时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637434/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com