无法在自定义 UITableViewCell 中设置属性 [英] Can't set properties in Custom UITableViewCell

查看:28
本文介绍了无法在自定义 UITableViewCell 中设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个自定义表格视图单元格 - 我有一个标题、实现和笔尖.在笔尖中,我将样式设置为自定义,在其上拖了一个标签,并在笔尖文件所有者中创建了一个插座.

I made a custom table view cell - i have a header, implementation, and nib. In the nib I set the style to custom, dragged a label on it and made an outlet in the nibs file owner.

从我的 UITableView 控制器我有这个代码:

From my UITableView Controller I have this code:

static NSString *CellIdentifier = @"adbActivityCell";

adbActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
    //cell =[[adbActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.price.text = [NSString stringWithFormat:@"cell #%d", indexPath.item + 1];

return cell;

如果我按原样运行它,XCode 会告诉我 UITableView 控制器不符合标签属性的键值(标签名为价格").如果我注释掉上面的两行并取消注释我的应用程序运行的那一行,但标签根本不显示,即使我为它设置了默认文本.

If I run this as is XCode tells me that the UITableView Controller is not key value compliant for the label property (the label is named "price"). If I comment out the two lines above and uncomment that one line my application runs, but the label doesn't show up at all, even if I set default text for it.

我花了很多时间在这里研究教程和问题,但都没有运气.

I've spent quite a lot of time researching tutorials and questions on here with no luck.

推荐答案

一切都与视图层次结构有关.

Its all about view hierarchy.

您必须将您的标签插座添加到自定义 UITableViewCell 中,因为它是您的标签在视图层次结构中的超级视图.

You have to add your label outlet to the custom UITableViewCell, because it is the superView of your label in view heirarchy.

这意味着标签包含在自定义单元格中,这就是您将出口添加到自定义单元格的原因.

That means label is contained in custom cell thats why you have add outlet to custom cell.

self.view->tableView->CustomCellView->UILabel

这篇关于无法在自定义 UITableViewCell 中设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆