I cannot set properties of a UILabel object.
The following code is in a - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method:
UILabel *label = (UILabel *)[cell viewWithTag:0];
label.text = [self.comments objectAtIndex:indexPath.row*2];
label.textColor = [UIColor cyanColor];
label.adjustsFontSizeToFitWidth = YES;
All I get is a runtime error on the fourth line (curiously, the previous lines are ok):
2011-06-29 11:23:57.641 Esker Monitor[94138:207] -[UITableViewCell setAdjustsFontSizeToFitWidth:]: unrecognized selector sent to instance 0x4e889e0
2011-06-29 11:23:57.642 Esker Monitor[94138:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setAdjustsFontSizeToFitWidth:]: unrecognized selector sent to instance 0x4e889e0'
Similiar label property modifications such as label.numberOfLines = 1; and label.minimumFontSize = 7.0; make the program crash too.
Is there something subtle I do not understand here?
If you want to access label that’s built in standard UITableViewCell then use cell’s textLabel property:
If you add your custom label to a cell I’d suggest using some non-zero tag for it as 0 is a default tag value so
can return any subview with 0 tag – not necessary you label. So assign some non-zero tag for your label and add it to cell’s contentView, not to the cell directly – that will help you to avoid some layout issues for example when cell goes to editing state. Later you can access that label the following way:
The reason that text and textColor methods still work is that UITableViewCell actually implements that methods (they are originated from times when cell did not expose its labels publicly and are deprecated since SDK 3.0)