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