[英]custom cell not appear in tableview
I use storyboards. 我使用情节提要。 I create new class for my Cell, in storyboard I write in Reuse Identifier "userFullCell". 我在“重用标识符”“ userFullCell”中编写的情节提要中为单元格创建新类。 My Cell class .h 我的细胞班.h
#import <UIKit/UIKit.h>
@interface UsefullLinksCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *image;
@property (weak, nonatomic) IBOutlet UILabel *shortTitle;
@end
.m default .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 and shortTitle connected in storyboard with UILabel and UIImageView in Cell 属性图像和shortTitle在故事板中与Cell中的UILabel和UIImageView连接
Then in my class with UITableView I import "UseFullLinksCell.h" and in viewDidLoad wrote: 然后在带有UITableView的类中,导入“ UseFullLinksCell.h”,并在viewDidLoad中写道:
[self.tableView registerClass:[UsefullLinksCell class] forCellReuseIdentifier:@"userFullCell"];
tableView is my outlet: tableView是我的出路:
@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;
}
So, cell initialize, but image and text not assigned, and after going end of method return 0x0000000 for cell. 因此,单元格初始化,但未分配图像和文本,并且在方法结束后为单元格返回0x0000000。 If I create for standart cell code in this method, I mean UITableViewCell *cell, there is all good. 如果我用这种方法为标准单元格代码创建,我的意思是UITableViewCell * cell,一切都很好。 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 PS对不起,我无法从情节提要中提供屏幕截图,因为我不是从计算机上书写的,如果需要,我稍后会提供图像
You should register the xib file (with registerNib:forCellReuseIdentifier:) where you made your custom cell, not the class. 您应该在创建自定义单元而非类的位置注册xib文件(使用registerNib:forCellReuseIdentifier :)。 Also, I don't know if this will make a difference or not, but replace this: 另外,我不知道这是否会有所作为,但请替换为:
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
With this (notice the forIndexPath: at the end of the method): 这样(注意方法末尾的forIndexPath:):
UsefullLinksCell *cell = (UsefullLinksCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
After Edit: 编辑后:
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. 如果将一个自定义单元格添加到情节提要中的表视图中,并为其赋予标识符“ userFullCell”,则您无需注册任何内容-实际上,注册您的类会使该类不起作用。 So, just comment out that register statement, and see if that works. 因此,只需注释掉该register语句,然后看看是否可行。
You have missed something: 您错过了一些东西:
NSArray *objects=[[NSBundle mainBundle]loadNibNamed:@"UsefullLinksCell" owner:self options:nil];
UsefullLinksCell *cell =[objects objectAtIndex:0];
this will give you the object of your custom cell. 这将为您提供自定义单元格的对象。
I think you have to do the following 我认为您必须执行以下操作
- (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;
}
Else all should work.. The thing is your components are not initialized anywhere, either it should be loaded from xib or, you have to initialize them manually. 否则所有这些都应该工作。问题是您的组件没有在任何地方初始化,应该从xib加载它,或者必须手动对其进行初始化。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.