當表格輸入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
這裏是它是instatiated(從我的視圖控制器類中摘錄):
-(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'
任何人都可以提供一些建議什麼檢查?謝謝! Vivian
什麼是錯誤消息(告訴我們實例和方法名稱)。 – Wain
您是否在使用自定義單元格的XIB?如果是這樣,可能是'UITableView'自動實例化'UITableViewCell'而不是你自定義的單元類。 (Yay,我<3 Interface Builder!) – 2013-05-19 17:32:05
向帖子中添加了錯誤信息 – Pheepster