[英]custom cell not appear in tableview
我使用情节提要。 我在“重用标识符”“ 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在故事板中与Cell中的UILabel和UIImageView连接
然后在带有UITableView的类中,导入“ UseFullLinksCell.h”,并在viewDidLoad中写道:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];
tableView是我的出路:
@property (weak, nonatomic) IBOutlet UITableView *tableView;
然后我尝试创建单元格:
- (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对不起,我无法从情节提要中提供屏幕截图,因为我不是从计算机上书写的,如果需要,我稍后会提供图像
您应该在创建自定义单元而非类的位置注册xib文件(使用registerNib:forCellReuseIdentifier :)。 另外,我不知道这是否会有所作为,但请替换为:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
这样(注意方法末尾的forIndexPath:):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
编辑后:
如果将一个自定义单元格添加到情节提要中的表视图中,并为其赋予标识符“ userFullCell”,则您无需注册任何内容-实际上,注册您的类会使该类不起作用。 因此,只需注释掉该register语句,然后看看是否可行。
您错过了一些东西:
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加载它,或者必须手动对其进行初始化。
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.