未显示自定义UITableViewCell的内容

时间:2014-06-11 17:21:57

标签: ios objective-c uitableview

我正在尝试创建自定义视图单元格。但它没有显示出来。 tableview单元格创建正确的数字,但不显示其内容。

VDCoreTableViewCell.h

@interface VDCoreTableViewCell : UITableViewCell
@property (weak, nonatomic)  UIImageView *cell_image;
@property (strong, nonatomic)  UILabel *title;
@property (weak, nonatomic)  UILabel *subTitle;

VDCoreTableViewCell.m

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.title = [[UILabel alloc] initWithFrame:self.bounds];

        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = self.title.bounds;
        gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor blackColor]CGColor], nil];
        [self.title.layer insertSublayer:gradientLayer atIndex:0];
    }
return self;
}

VDViewController.h

 @interface VDViewController : VDCoreViewController <UITableViewDelegate, UITableViewDataSource>
 @property (strong, nonatomic)  UITableView *tableView;
 @end

VDViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.tableView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth];
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.tableView registerClass: [VDCoreTableViewCell class] forCellReuseIdentifier:@"cell_id"];
    [self.view addSubview:self.tableView];

}


 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     VDCoreTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil)
    {
        cell = [[VDCoreTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }

    cell.title.text = @"test";
    cell.subTitle.text = @"test";

}

请帮助。

1 个答案:

答案 0 :(得分:0)

您必须将titleLabel添加到您的手机

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
   if (self) {
    self.title = [[UILabel alloc] initWithFrame:self.bounds];

    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.title.bounds;
    gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor]CGColor], (id)[[UIColor blackColor]CGColor], nil];
    [self.title.layer insertSublayer:gradientLayer atIndex:0];
    [self.contentView addSubview:self.title]; //Add your label what you have created to content view
  }
  return self;
}