0

我使用故事板。我在情节提要中为我的 Cell 创建了新类,我用 Reuse Identifier “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
}

属性图像和短标题在情节提要中与单元格中的 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对不起,我不能提供故事板的截图,因为我不是用我的电脑写的,如果你愿意,我稍后会提供图片

4

3 回答 3

6

您应该在您创建自定义单元格的地方注册 xib 文件(使用 registerNib:forCellReuseIdentifier:),而不是类。另外,我不知道这是否会有所作为,但请替换它:

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

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

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

编辑后:

如果在情节提要的表格视图中添加了一个自定义单元格,并为其指定了标识符“userFullCell”,那么您不需要注册任何东西——事实上,注册您的类会使它不起作用。所以,只需注释掉该 register 语句,看看它是否有效。

于 2013-02-14T07:34:51.563 回答
1

你错过了一些东西:

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

这将为您提供自定义单元格的对象。

于 2013-02-14T07:27:29.947 回答
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 加载,要么你必须手动初始化它们。

希望这可以帮助。

于 2013-02-14T07:52:41.183 回答