自定义单元格不会出现在tableview中

我使用故事板。 我为我的单元格创build新的类,在故事板中,我使用重用标识符“userFullCell”编写。 我的单元类.h

#import <UIKit/UIKit.h> @interface UsefullLinksCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *image; @property (weak, nonatomic) IBOutlet UILabel *shortTitle; @end 

.m默认

 #import "UsefullLinksCell.h" @implementation UsefullLinksCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } 

属性图像shortTitle在故事板中与UILabel和UIImageView在Cell中连接

然后在我的类与UITableView我导入“UseFullLinksCell.h”,并在viewDidLoad写道:

 [self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"]; 

tableView是我的出口:

 @property (weak, nonatomic) IBOutlet UITableView *tableView; 

然后我尝试创build单元格:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath { static NSString *CellIdentifier = @"userFullCell"; UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSDictionary *info = resourceArray[indexPath.row]; if(info){ cell.image.image = info[@"logo"]; cell.shortTitle.text = info[@"title"]; } return cell; } 

所以,单元格初始化,但是图像和文本没有分配,并且在方法结束之后返回单元格0x0000000。 如果我在这个方法中创build了标准的单元代码,我的意思是UITableViewCell *单元,这是一切都很好。 我可以犯错的地方? PS抱歉,我不能给出故事板的截图,因为我不是从我的电脑写的,如果你愿意的话,我可以稍后提供图片

你应该注册的xib文件(与registerNib:forCellReuseIdentifier :)你做你的自定义单元格,而不是类。 此外,我不知道这是否会有所作为,但取而代之:

 UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

用这个(注意forIndexPath:在方法的末尾):

 UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

编辑后:

如果在故事板的表格视图中添加了一个自定义的单元格,并给它一个标识符“userFullCell”,那么你不需要注册任何东西 – 事实上,注册你的类别使它不起作用。 所以,只是注释掉注册声明,看看是否有效。

你错过了一些东西:

  NSArray *objects=[[NSBundle mainBundle]loadNibNamed:@"UsefullLinksCell" owner:self options:nil]; UsefullLinksCell *cell =[objects objectAtIndex:0]; 

这会给你你的自定义单元格的对象。

我认为你必须做到以下几点

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code image=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,50.0,50.0)]; shortTitle=[[UILabel alloc] initWithFrame:CGRectMake(60.0,0.0,200.0,50.0)]; } return self; } 

否则所有应该工作..事情是你的组件不是初始化任何地方,要么它应该从XIB加载,或者,你必须手动初始化它们。

希望这可以帮助。