2013-03-21 78 views
2

我正在使用TableView xib,并且所有委托和数据源似乎都正常运行。 如果我设置self.textLabel.text,它会正确显示正确的tableviews的数量,但我需要显示我的自定义TableViewCell。无法让我的自定义UITableViewCell显示

我创建了一个HistoryCell.xib,其中只有一个tableviewcell。 我创建了一个UITableViewCell类“HistoryCell.h/HistoryCell.m”,它被设置为HistoryCell.xib的文件所有者。 连接了UILabels到HistoryCell.h UILabel statusLabel UILabel nameLabel UILabel timeLabel

在我的主视图控制器类int他cellForRowAtIndexPath

我把在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

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

cell.nameLabel.text = @"Donald Duck"; 

return cell; 

} 

我在做什么错?

谢谢!

+1

你可以看看链接上的教程,但它基本上是@veddermatic回答的。 http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/ – CainaSouza 2013-03-21 20:35:14

回答

8

你应该这样注册笔尖(可能是在viewDidLoad中):

[self.tableView registerNib:[UINib nibWithNibName:@"HistoryCell" bundle:nil ] forCellReuseIdentifier:@"historyCellType"]; 

然后在你的cellForRowAtIndexPath方法,使用这种新的方式,你不需要,如果细胞==零条款:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"historyCellType" forIndexPath:indexPath]; 

cell.nameLabel.text = @"Donald Duck"; 

return cell; 

}

+0

这很聪明!因为未捕获的异常'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:这个类不是键值编码 - 这个类不是键值编码,符合密钥名称标签“。 – Alan 2013-03-21 21:24:58

+0

@Alan,这可能意味着你的xib文件有问题。确保你的插座真的叫做nameLabel,并且你没有任何黄色的警告三角形,如果你右键点击单元格以显示所有连接的窗口。另外,在定义单元格后记录cellForRowAtIndexPath,记录单元格,并确保找回HistoryCell。 – rdelmar 2013-03-21 21:30:08

+0

没有任何代码,所以我猜测存在某种配置问题...... UITableViewCell是否假设在视图内?我只有在xib内的TableViewCell和UITableViewCell内的3个UILabel。另外,文件所有者是否假设为主控制器类或“HistoryCell.h/HistoryCell.m” 我检查了命名并且命名正确,当我将鼠标悬停在HistoryCell.h中的连接点上时,它突出显示xib中正确的标签... – Alan 2013-03-21 21:47:27

4

你需要反序列化的实际XIB数据:否则,它只是使用“默认”细胞

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:@"HistoryXIBName" owner:self options:nil]; 
    cell = (HistoryCell *)[cellnib objectAtIndex:0]; 
} 

编辑:另外,如果你使用的是故事板,动态电池原型删除需要创建NIB文件细胞

编辑2日(如果这是你的选择。): 你可以试试这个,以及(假设您正在使用ARC):

HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:@"HistoryXIBName" bundle:nil]; 
    cell = (HistoryCell *)*aNewCell.view; 
} 

有利用网点,在包含视图的XIB文件,你的这是一个有点更多地参与,我曾经使用时iOS4的是电流的另一种方法。如果编辑后的版本不适合你,我会挖掘一个旧项目并记住我是如何做到的。

+0

你好,感谢您的及时帮助! 我试过这段代码,但当它到达时,应用程序崩溃: 2013-03-21 17:00:36。212 AppName的[3133:C07] ***终止应用程序由于未捕获的异常 'NSUnknownKeyException',原因: '[的setValue:forUndefinedKey:]:这个类不是密钥值编码兼容的关键nameLabel' – Alan 2013-03-21 21:01:52

+0

艾伦:我编辑了第二种方法来尝试。另外,你能告诉我你的iOS版本是什么? – veddermatic 2013-03-21 21:24:09

+0

我的目标是6.1。好的,我会试试看。 – Alan 2013-03-21 21:26:14

0

当您使用xib的tableViewCell时,实例化从xib加载。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

    static NSString *CellIdentifier = @"historyCellType"; 
    HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[NSBundle mainBundle]loadNibNamed:@"HistoryCell" 
             owner:nil 
             options:nil]lastObject]; 
    } 

    cell.nameLabel.text = @"Donald Duck"; 

    return cell; 

    } 
0
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *[email protected]"cell1"; 
    customcell *cell = 
     (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier]; 

    if (cell==nil) { 

    [tableView registerNib:[UINib nibWithNibName:@"Customcell" bundle:nil] 
       forCellReuseIdentifier:cellidentifier]; 

    cell = 
    (customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier 
       forIndexPath:[NSIndexPath indexPathForItem:indexPath.row 
                inSection:indexPath.section]]; 
    } 

    return cell; 
}