2010-06-11 34 views
0

我創建了一個自定義的UITableViewCell,但是當我出列的單元格,有時它拋出一個NSInvalidArgumentException爲什麼自定義UITableViewCell *有時*會導致NSInvalidArgumentException?

[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x3b4e7f0

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[UITableViewCell nameLabel]: unrecognized selector sent to instance 0x3b4e7f0'

現在,我的自定義UITableViewCell確實有一個屬性nameLabel,所以我很困惑,爲什麼它被扔這個錯誤。下面是我用出列單元格中的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSUInteger row = [indexPath row]; 
    CTMenuItemVO* key = [[[self retrieveCartItems] allKeys] objectAtIndex:row]; 
    NSNumber* quantity = [[self retrieveCartItems] objectForKey:key]; 
    static NSString* SectionsTableIdentifier = @"SectionsTableIdentifier2"; 
    OrderItemCell* cell = (OrderItemCell*)[tableView dequeueReusableCellWithIdentifier: 
          SectionsTableIdentifier]; 

    if (cell == nil) { 
     NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"OrderItemCell"  
                    owner:nil 
                    options:nil]; 

     for(id currentObject in topLevelObjects) 
     { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (OrderItemCell*) currentObject; 
       break; 
      }    
     } 
    } 

    cell.nameLabel.text = key.Name; 
    cell.qtyLabel.text = [quantity stringValue]; 

    return cell; 
} 

UPDATE

改變isKindOfClass:[UITableViewCell class]支票。OrderItemCell產生另一個錯誤:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

我想這是因爲它的葉子沒有分配類的for循環。

回答

1

原來有兩個不同副本名爲OrderItemCell。不知道如何將doppelganger放入我的文件夾結構中(儘管我的工作區中只有一個引用了該文件夾),但是一旦我移除了冒名頂替者,它每次都像魅力一樣工作。

1

OrderItemCell類型的所有對象都在topLevelObjects?有可能您將一個不同的對象分配給cell,將它類型化爲不同的對象並因此導致無法識別的選擇器錯誤。

+0

對不起,沒有。我得到另一個:由於未捕獲的異常'NSInternalInconsistencyException',原因:'UITableView dataSource必須從tableView:cellForRowAtIndexPath:返回一個單元格,假設它沒有找到任何單元格而離開'for'循環這種特殊類型。 – 2010-06-11 23:35:23

0

將[UITableViewCell類]更改爲[OrderItemCell類]。

相關問題