2012-05-21 24 views
0

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

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

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

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

你如何在你的cellForRowAtIndexPath中创建/离队:你在故事板中设置了重用标识符吗? –

+0

啊你很强壮,我忘了在故事板中设置cellidentifier,我忘了在我的'cellforRowatIndexPath'函数中更新它。现在工作 – John

回答

2

正如大卫已经提到,这里的一个遗漏的部分是,你需要引用的故事板中指定的小区的标识:

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

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