我有一个TableView,我在其中添加自定义UILabels到UITableViewCells。 tableView加载正常,但当它试图设置UILables的文本时,在滚动应用程序后崩溃了一个单元格。代码如下:UITableViewController在tableView中崩溃:cellForRowAtIndexPath:
#define STYLE_NUMBER_TAG 0
#define COLORWAY_TAG 1
#define SIZE_TAG 2
#define QUANTITY_TAG 3
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the managedObject
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
OrderLineItem *item = (OrderLineItem *)managedObject;
static NSString *CellIdentifier = @"lineItemCell";
UILabel *styleNumberLabel, *colorwayLabel, *sizeLabel, *quantityLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
quantityLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 70, 20)] autorelease];
quantityLabel.tag = QUANTITY_TAG;
[cell.contentView addSubview:quantityLabel];
styleNumberLabel = [[[UILabel alloc] initWithFrame:CGRectMake(85, 5, 70, 20)] autorelease];
styleNumberLabel.tag = STYLE_NUMBER_TAG;
[cell.contentView addSubview:styleNumberLabel];
colorwayLabel = [[[UILabel alloc] initWithFrame:CGRectMake(165, 5, 70, 20)] autorelease];
colorwayLabel.tag = COLORWAY_TAG;
[cell.contentView addSubview:colorwayLabel];
sizeLabel = [[[UILabel alloc] initWithFrame:CGRectMake(245, 5, 70, 20)] autorelease];
sizeLabel.tag = SIZE_TAG;
[cell.contentView addSubview:sizeLabel];
} else {
styleNumberLabel = (UILabel *)[cell.contentView viewWithTag:STYLE_NUMBER_TAG];
colorwayLabel = (UILabel *)[cell.contentView viewWithTag:COLORWAY_TAG];
sizeLabel = (UILabel *)[cell.contentView viewWithTag:SIZE_TAG];
quantityLabel = (UILabel *)[cell.contentView viewWithTag:QUANTITY_TAG];
}
// Configure the cell...
styleNumberLabel.text = item.style.styleNumber; //CRASHES HERE when dequeueing
colorwayLabel.text = item.colorway;
sizeLabel.text = item.size;
quantityLabel.text = [item.quantity stringValue];
return cell;
}
感谢
你是否在控制台中看到一些错误信息? – vikingosegundo 2010-09-30 23:08:20
item.style.styleNumber是什么类型? – vikingosegundo 2010-09-30 23:08:54
错误是:***由于未捕获的异常'NSInvalidArgumentException',原因:'*** - [UITableViewCellContentView setText:]:无法识别的选择器发送到实例0xc90abe0' – Alpinista 2010-09-30 23:19:17