0

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

谢谢!

代码

自定义单元格.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 的类)

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    static NSString *CellIdentifier = @"CellIdentifier";
    CustomCell *cell=  [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) 
    {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.userName=@"Adam Scott";
    cell.userImage=[UIImage imageNamed:@"adam.png"];
    return cell;
}
4

4 回答 4

1

试试这样

 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;

}
于 2013-10-03T01:59:58.893 回答
1

这是我为自定义单元所做的:

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".

于 2013-10-03T00:07:58.297 回答
1

我会在该类中添加两种方法:

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

    // mark view as dirty
    [self setNeedsDisplay];
}

对 userImage 做同样的事情。

然后通过调用设置它:

[cell setUserName:@"Foo"];
于 2013-10-03T00:09:55.563 回答
1

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

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

对此

cell.userName.text=@"Adam Scott";
cell.userImage.image=[UIImage imageNamed:@"adam.png"];
于 2013-10-03T00:30:39.850 回答