2013-05-19 14 views
6

當表格輸入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

+1

什麼是錯誤消息(告訴我們實例和方法名稱)。 – Wain

+0

您是否在使用自定義單元格的XIB?如果是這樣,可能是'UITableView'自動實例化'UITableViewCell'而不是你自定義的單元類。 (Yay,我<3 Interface Builder!) – 2013-05-19 17:32:05

+0

向帖子中添加了錯誤信息 – Pheepster

回答

11

如果您已將自定義單元格定義在單獨的NIB文件中,則使用- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier是一個好主意。問題是你傳遞了一個NSString而不是一個UINib對象。

而是執行此操作:

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

但是因爲你使用的原型細胞,而不是一個單獨的NIB,有沒有必要做這個的。相反,您只需確保原型單元的重用標識符已正確設置(在本例中爲「favoriteCell」)。

+0

有用的答案,謝謝! –

相關問題