自定义单元格未出现在表格视图中 [英] custom cell not appear in tableview
问题描述
我使用故事板.我为我的 Cell 创建了新类,在故事板中我用重用标识符userFullCell"编写.我的 Cell 类 .h
I use storyboards. I create new class for my Cell, in storyboard I write in Reuse Identifier "userFullCell". My Cell class .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
}
property image 和 shortTitle 在情节提要中与 UILabel 和 UIImageView 连接在 Cell 中
property image and shortTitle connected in storyboard with UILabel and UIImageView in Cell
然后在我的 UITableView 类中我导入UseFullLinksCell.h"并在 viewDidLoad 中写道:
Then in my class with UITableView I import "UseFullLinksCell.h" and in viewDidLoad wrote:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];
tableView 是我的出口:
tableView is my outlet:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
然后我尝试创建单元格:
and then I try to create cells:
- (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.如果我用这种方法为标准单元格代码创建,我的意思是 UITableViewCell *cell,一切都很好.我哪里会出错?PS 抱歉我不能提供故事板的截图,因为我不是用我的电脑写的,如果你想要,我稍后提供图片
So, cell initialize, but image and text not assigned, and after going end of method return 0x0000000 for cell. If I create for standart cell code in this method, I mean UITableViewCell *cell, there is all good. Where I could make mistake? PS Sorry I can not give screenshots from storyboard, because I write from not my computer, if you want, I provide images later
推荐答案
您应该注册您制作自定义单元格的 xib 文件(使用 registerNib:forCellReuseIdentifier:),而不是类.另外,我不知道这是否会有所作为,但请替换:
You should register the xib file (with registerNib:forCellReuseIdentifier:) where you made your custom cell, not the class. Also, I don't know if this will make a difference or not, but replace this:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
有了这个(注意方法末尾的 forIndexPath:):
With this (notice the forIndexPath: at the end of the method):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
编辑后:
如果在情节提要中的表格视图中添加了一个自定义单元格,并为其指定了标识符userFullCell",那么您不需要注册任何东西——事实上,注册您的类使其不起作用.所以,只需注释掉那个 register 语句,看看它是否有效.
If added a custom cell to your table view in the storyboard, and gave it the identifier "userFullCell", then you don't need to register anything -- in fact, registering your class makes it not work. So, just comment out that register statement, and see if that works.
这篇关于自定义单元格未出现在表格视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!