Zhen
Zhen

Reputation: 12431

Crash when setting text to a custom table cell label

My app crashes when I want to set the text of the label belonging to a table cell. My code snippet is as follows:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) 
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:simpleTableIdentifier];
    [cell autorelease];

    CGRect splitMethodLabelRect = CGRectMake(160, 6, 50, 30); 
    UILabel *splitMethodLabel = [[UILabel alloc] initWithFrame:splitMethodLabelRect]; 
    splitMethodLabel.textAlignment = UITextAlignmentLeft; 
    splitMethodLabel.font = [UIFont systemFontOfSize:13]; 
    splitMethodLabel.tag = kSplitMethodTag;
    [cell.contentView addSubview: splitMethodLabel]; 
    [splitMethodLabel release];

}

UILabel *splitMethodName = (UILabel *)[cell.contentView viewWithTag:kSplitMethodTag]; 

//app crashes at this point
splitMethodName.text = @"Test"; 

The issue seems to be at the point when I am setting the text. Stacktrace below:

2011-04-21 15:11:10.820 BillSplitter[3021:707] -[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680
2011-04-21 15:11:10.829 BillSplitter[3021:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView setText:]: unrecognized selector sent to instance 0x18a680'

Any advise on this is greatly appreciated!

Zhen

Upvotes: 1

Views: 4045

Answers (2)

EmptyStack
EmptyStack

Reputation: 51374

It seems that the tag value you've set to splitMethodLabel could be causing the problem.
Just change the tag value to something else and check if it crashes still.

Upvotes: 6

Nick Weaver
Nick Weaver

Reputation: 47241

Ah the problem is: you are adding your own label to the cell as subview, but there is not a property given which references splitMethodName. The label is in the view hierarchy of the cell but you don't have a reference to access it.

You could fix this by subclassing UITableViewCell and add your label as a property. Use your custom class then. Override initWithStyle, pass the parameters to super, then create you label, add as subview AND assign to your property.

Upvotes: 2

Related Questions