简体   繁体   English

自定义表格视图单元格

[英]Custom Table View Cell

I have created a custom UITableView Cell class for my UITableView. 我为我的UITableView创建了一个自定义UITableView Cell类。 However, I cannot set the values within the cell for some reason. 但是,由于某种原因,我无法在单元格内设置值。 As in the values such as userName and userImage do not show when the table is loaded. 如在表加载时一样,诸如userName和userImage之类的值不会显示。 This problem has been going for a few days. 这个问题已经持续了几天。 Below is my code, I used dummy values for sake of simplicity. 下面是我的代码,为简单起见,我使用了伪值。 If someone can please let me know what I am doing wrong, it would be appreciated. 如果有人可以让我知道我做错了,将不胜感激。

Thanks! 谢谢!

CODE

CustomCell.h 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 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 (The class with the UITableView that calls the custom cell) 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;
}

Here is what I did for a custom cell: 这是我对自定义单元格所做的操作:

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

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

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

Assuming you hooked up everything right in IB, I think you need to access the label's text using cell.labelName.text=@"Adam Scott" . 假设您正确连接了IB中的所有内容,我认为您需要使用cell.labelName.text=@"Adam Scott"访问标签的文本。

I would add two methods to that class: 我将向该类添加两个方法:

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

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

Do the same thing for the userImage. 对userImage做同样的事情。

Then set it by calling: 然后通过调用设置它:

[cell setUserName:@"Foo"];

You can try this. 你可以试试看 Change your lines of code 更改代码行

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

to this 对此

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

try like this 这样尝试

 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM