Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my UITableViewCell have a textLabel property?

I have a UITableView, which I want to do terribly normal things with, such as display text in each row.

To do this, I've implemented the usual delegate method tableView:cellForRowAtIndexPath:. This is called as expected. According to the docs, I'm supposed to create a UILabel object and set it as the textLabel property on the new UITableCell.

But I crash with unrecognized selector for setTextLabel: -- the compiler duly warns me as well that it's not there. The deprecated method setText: is present and works fine (with warning). I definitely seem to building against the 3.0 libraries (I don't get a choice in the dropdown of other targets. So I'm puzzled. What am I missing?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    UILabel * label = [[[UILabel alloc] init] autorelease];
    [label setText:@"Foo."];
    [cell setTextLabel:label];   // BOOM.
    return cell;
}

In Swift it may also display the error:

Cannot assign to property: 'textLabel' is a get-only property

like image 368
Ben Zotto Avatar asked Jan 25 '10 16:01

Ben Zotto


1 Answers

The textLabel property is marked readonly. You have to set textLabel.text instead:

cell.textLabel.text = @"some Text";

like image 151
AlexVogel Avatar answered Sep 19 '22 16:09

AlexVogel