2013-05-16 152 views
0

我创建了一个自定义tableviewcell。该班有3个标签。使用主视图控制器模板开始,我更改了故事板中的默认tableviewcell以引用我的新自定义单元格,我还将类型更改为自定义,并将标识符更改为“CustomTableCell”。我也修改了我的cellForRowAtIndexPath方法如下...自定义tableviewcell不显示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"CustomTableCell"; 

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (!cell) 
    { 
     cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    Item *currentItem = _objects[indexPath.row]; 
    cell.nameLabel.text = [currentItem name]; 
    cell.vegLabel.text = @"V"; 
    return cell; 
} 

自定义单元格的头文件

#import <UIKit/UIKit.h> 

@interface CustomTableCell : UITableViewCell 

@property (nonatomic, weak) IBOutlet UILabel *nameLabel; 
@property (nonatomic, weak) IBOutlet UILabel *vegLabel; 
@property (nonatomic, weak) IBOutlet UILabel *priceLabel; 

@end 

Eveything似乎在我的故事板正确连接。当我调试时,我可以看到单元格具有我自定义单元格的属性。然而,当我运行应用程序的每一行都是空白的。 tableviewcell在故事板中使用正确的标识符。我只是看不到我错过了什么。任何帮助,将不胜感激。谢谢。

Identifier in storyboard Connections in storyboard

+0

你有答案吗? –

回答

1

你是不是从mainbundle加载自定义单元格。所以你需要加载它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *cellIdentifier = @"CustomTableCell"; 

    CustomTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    // Add this line in your code 
    cell = [[[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil]objectAtIndex:0]; 

    if (!cell) 
    { 
     cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    Item *currentItem = _objects[indexPath.row]; 
    cell.nameLabel.text = [currentItem name]; 
    cell.vegLabel.text = @"V"; 
    return cell; 
} 
+0

我不使用笔尖。我正在使用故事板。当我试图说我得到一个错误,说它无法加载笔尖。我用一个不同的名称重新创建了自定义单元类,并将其挂钩,现在它似乎可以工作。我仍然不知道为什么以前没有。 – BON

+0

我无法理解故事板。请亲爱的.. –