Ios 自定义表视图单元格

Ios 自定义表视图单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我已经为我的UITableView创建了自定义UITableView单元格类。但是,由于某些原因,我无法在单元格内设置值。如中所示,在加载表时,userName和userImage等值不会显示。这个问题已经持续了几天了。下面是我的代码,为了简单起见,我使用了伪值。如果有人能让我知道我做错了什么,我将不胜感激 谢谢 代码 CustomCell.h @interface CustomCell : UITableViewCell { IBOutlet UILabel *userName;

我已经为我的UITableView创建了自定义UITableView单元格类。但是,由于某些原因,我无法在单元格内设置值。如中所示,在加载表时,userName和userImage等值不会显示。这个问题已经持续了几天了。下面是我的代码,为了简单起见,我使用了伪值。如果有人能让我知道我做错了什么,我将不胜感激

谢谢

代码

CustomCell.h

@interface CustomCell : UITableViewCell
{
    IBOutlet UILabel *userName;
    IBOutlet UIImageView *userImage;
}

@property(strong,nonatomic) IBOutlet UILabel *userName;
@property(strong,nonatomic) IBOutlet UIImageView *userImage;

@end
CustomCell.m

#import "CustomCell.h"
@interface CustomCell ()
@end

@implementation CustomCell
@synthesize userName;
@synthesize userImage;

  -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Custom initialization
        NSArray *nibArray= [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        self= [nibArray objectAtIndex:0];
    }
    return self;
}

-(void) setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}
@end
ViewController.m(具有调用自定义单元格的UITableView的类)


以下是我为自定义单元格所做的操作:

ReceiverCell *receiverCell = (ReceiverCell *)[tableView dequeueReusableCellWithIdentifier:@"receiverCellIdentifier"];

if (receiverCell == nil) {
    receiverCell = [[ReceiverCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"receiverCellIdentifier"];
}

receiverCell.receiverLabel.text=@"The Text";

假设您在IB中正确地连接了所有内容,我认为您需要使用
cell.labelName.text=@“Adam Scott”

访问标签文本,以下是我为自定义单元格所做的操作:

ReceiverCell *receiverCell = (ReceiverCell *)[tableView dequeueReusableCellWithIdentifier:@"receiverCellIdentifier"];

if (receiverCell == nil) {
    receiverCell = [[ReceiverCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"receiverCellIdentifier"];
}

receiverCell.receiverLabel.text=@"The Text";

假设您在IB中正确地连接了所有内容,我认为您需要使用
cell.labelName.text=@“Adam Scott”

访问标签文本,我将向该类添加两个方法:

- (void)setUserName:(NSString *)aUserName {
    [userNameLabel setText:aUserName];

    // mark view as dirty
    [self setNeedsDisplay];
}
对userImage执行相同的操作

然后通过调用以下命令进行设置:

[cell setUserName:@"Foo"];

我将向该类添加两个方法:

- (void)setUserName:(NSString *)aUserName {
    [userNameLabel setText:aUserName];

    // mark view as dirty
    [self setNeedsDisplay];
}
对userImage执行相同的操作

然后通过调用以下命令进行设置:

[cell setUserName:@"Foo"];

你可以试试这个。更改代码行

cell.userName=@"Adam Scott";
cell.userImage=[UIImage imageNamed:@"adam.png"];
对此

cell.userName.text=@"Adam Scott";
cell.userImage.image=[UIImage imageNamed:@"adam.png"];

你可以试试这个。更改代码行

cell.userName=@"Adam Scott";
cell.userImage=[UIImage imageNamed:@"adam.png"];
对此

cell.userName.text=@"Adam Scott";
cell.userImage.image=[UIImage imageNamed:@"adam.png"];
像这样试试

 CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil) {
    NSArray *top = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    for (id t in top) {
        if ([t isKindOfClass:[UITableViewCell class]]) {
            cell =(CustomCell*)t;
            break;
        }
    }
}
cell.username.text = @"blabla";
cell.userImage.image = [UIImage imageNamed:@"avatar.png"];

return cell;

}
像这样试试

 CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
if (cell == nil) {
    NSArray *top = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    for (id t in top) {
        if ([t isKindOfClass:[UITableViewCell class]]) {
            cell =(CustomCell*)t;
            break;
        }
    }
}
cell.username.text = @"blabla";
cell.userImage.image = [UIImage imageNamed:@"avatar.png"];

return cell;

}

我需要更多的信息来帮助你。是否在interface builder中构建单元?您是否已导入CustomCell.h?下面是我给出的另一个答案的链接,可能会有所帮助:嗯。。。您需要UILabels或一些容器来显示文本。设置单元格变量不会显示任何文本。@UILabels通过界面生成器连接。可以吗?Thanks@DavidL是的,我正在通过IB构建单元。我已经在identity inspector下将类名更改为CustomCell。感谢您,但在设置变量时,UILabel的文本永远不会被设置。为什么要将字符串设置为UILabel(用户名)?还有UIImageView的图像?我需要更多信息来帮助你。是否在interface builder中构建单元?您是否已导入CustomCell.h?下面是我给出的另一个答案的链接,可能会有所帮助:嗯。。。您需要UILabels或一些容器来显示文本。设置单元格变量不会显示任何文本。@UILabels通过界面生成器连接。可以吗?Thanks@DavidL是的,我正在通过IB构建单元。我已经在identity inspector下将类名更改为CustomCell。感谢您,但在设置变量时,UILabel的文本永远不会被设置。为什么要将字符串设置为UILabel(用户名)?和一个图像到一个UIImageView?