Reputation: 1214
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?
Upvotes: 0
Views: 1578
Reputation: 72
In the header of your TableViewCell class, you should create
@property (nonatomic, retain) UILabel *textLabel;
In the TableViewCell implementation class, make sure that you have @synthesize textLabel
And in the cellForRowAtIndex, you create the cell from that TableViewCell class. And you can get the cell with this function
MyCustomizedCell *cell = (MyCustomizedCell *)[tableView dequeueReusableCellWithIdentifier:(NSString *) myIndent];
And so on, you can use every object inside of that TableViewCell as easy as
cell.textLabel.adjustsFontSizeToFitWidth = YES;
or
[cell.textLabel setAdjustsFontSizeToFitWidth:YES];
Upvotes: 0
Reputation: 19418
give proper tags to your sub views and then try . Or if want to set default label of table view cell then u should use cell.textLable
Upvotes: 0
Reputation: 2897
You can do it like below way:
[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
[cell.textLabel setTextColor:[UIColor colorWithRed:222.0/255.0 green:221.0/255.0 blue:221.0/255.0 alpha:1.0]];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
You can adjust any property like this way.
Let me know in case of any difficulty.
Cheers.
Upvotes: -2
Reputation: 51374
Actually [cell viewWithTag:0]
returns UITableViewCell
itself. So, just change the tag
of UILabel to something else, like 100, 101, etc.,
Upvotes: 2
Reputation: 170839
If you want to access label that's built in standard UITableViewCell then use cell's textLabel property:
UILabel *label = cell.textLabel;
...
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
[cell viewWithTag:0];
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:
UILabel *label = (UILabel*)[cell.contentView viewWithTag:kSomeNonZeroTag];
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)
Upvotes: 6
Reputation: 12787
You are making label with different object(tag 0 is for cell not for label). Check the object which you make for UILabel label, it is not UILabel type. it takes UITableViewCell type.
Thats why it gives crash.
Upvotes: 1
Reputation: 3637
You need to use cell.textLabel for accessing label, right now, it gives error due to "label" itself having reference of UITableViewCell.
Upvotes: 0