0

我是 iphone 开发的新手。我正在使用带有情节提要的 xCode 4.2。我试图让我的表格视图中的单元格显示几个 uilabels。他们只是没有出现在我的模拟器中。但原版cell.textLabel出现了。

这就是我所做的:我创建了一个扩展 UITableViewCell 的新 customcell.h/.m 文件。我在类中添加了一些实例变量。我去了情节提要,在 tableviewcell 的身份检查器类中,我将其更改为指向 customcell。我将一些 UIlabels 拖放到我的新单元格中。然后我去连接检查器拖动那些带有线条的“加号”图标以将我的新 uilabels 连接到我的实例变量。

当我运行我的模拟器时,我看不到任何新的 ui 标签。我只看到原始的 uilabel cell.textLabel。我可以以编程方式获取和设置cell.mylabel1cell.mylabel2...,它们是新标签,但是嘿只是没有出现。

添加详细信息 这是我的 tableviewcontroller 的 cellForRowAtIndexpath 函数。

static NSString *CellIdentifier = @"Cell"; // this was the error, i needed to change this to CustomCell, or whatever it is in my storyboard

CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.mylabel1.text = @"hello"; // doesn't appear in simulator
cell.textLabel.text = @"world"; // does apepar in the simulator
4

1 回答 1

2

正如大卫所暗示的那样,这里缺少的一个部分是您需要引用情节提要中指定的单元格的标识符:

首先,在情节提要的“属性检查器”中指定一个标识符。您可以通过选择 UITableViewCell 并添加标识符“ExampleCell”(例如)来执行此操作。其次,在创建 CustomCell 时在代码中引用此标识符。

static NSString *CellIdentifier = @"ExampleCell";
CustomCell *cell = (CustomCell *) [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
}
于 2012-05-21T22:44:32.640 回答