【问题标题】:Labels in Custom Cell do not appear自定义单元格中的标签不显示
【发布时间】:2012-05-21 22:05:01
【问题描述】:

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

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

当我运行模拟器时,我看不到任何新的 ui 标签。我只看到cell.textLabel 的原始uilabel。我可以以编程方式获取和设置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

【问题讨论】:

  • 您如何在cellForRowAtIndexPath: 中创建/出列单元格?您是否在情节提要中设置了重用标识符?
  • 啊,你是对的,我忘了在情节提要中设置单元标识符,我忘了在我的cellforRowatIndexPath 函数中更新它。现在工作

标签: objective-c uitableview


【解决方案1】:

正如 David 所暗示的,这里缺少的一点是您需要引用情节提要中指定的单元格的标识符:

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

static NSString *CellIdentifier = @"ExampleCell";
CustomCell *cell = (CustomCell *) [tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:CellIdentifier];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多