2014-06-11 120 views
0

我正在嘗試創建自定義視圖單元格。但它沒有顯示。 tableview單元格被創建正確的數字,但它們的內容不顯示。自定義UITableViewCell的內容未顯示

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

} 

請幫忙。

+0

哪裏是cellAtRowIndexPath先生 – meda

+0

我編輯的代碼 – zontragon

回答

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; 
} 
相關問題