自定义单元格中的标签不会出现

时间:2012-05-21 22:05:01

标签: objective-c uitableview

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

这就是我的所作所为: 我创建了一个新的customcell.h / .m文件,扩展了UITableViewCell。我在课堂上添加了一些实例变量。我去了故事板和tableviewcell的身份检查员的班级,我把它改成指向customcell。我将一些UIlabels拖放到我的新单元格中。然后我去了连接检查器,用线条拖动那些“加”图标,将我的新uilabels连接到我的实例变量。

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

Additioanl详细信息 这是我的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

1 个答案:

答案 0 :(得分:2)

正如大卫所提到的,这里缺少的一个是你需要引用故事板中指定的单元格的标识符:

首先,在故事板中的“属性检查器”中指定标识符。您可以通过选择UITableViewCell并添加标识符“ExampleCell”(例如)来完成此操作。其次,在创建CustomCell时在代码中引用此标识符。

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