自定义表格视图单元格 [英] Custom Table View Cell

查看:32
本文介绍了自定义表格视图单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a custom UITableView Cell class for my UITableView. 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. 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.

谢谢!

代码

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 的类)

ViewController.m (The class with the UITableView that calls the custom cell)

-(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";

假设您在 IB 中正确连接了所有内容,我认为您需要使用 cell.labelName.text="Adam Scott" 访问标签的文本.

Assuming you hooked up everything right in IB, I think you need to access the label's text using cell.labelName.text=@"Adam Scott".

这篇关于自定义表格视图单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆