开发者

Why doesn't my UITableViewCell have a textLabel property?

开发者 https://www.devze.com 2022-12-17 06:49 出处:网络
I have a UITableView, which I want to do terribly normal things with, such as display text in each row.

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


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

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


You can use:

cell.textlabel.text =[NSString StringWithFormat:@"%d", yourint];

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号