2011-07-24 1 views
2

私はそれが何百万回も尋ねられたことを知っています。たぶん、Imはあまりにも疲れましたが、私はこのラベルのテキストが更新されない理由を理解できません。相続人は私のコードラベルはサブクラス化した後にテキストを表示しませんUITableViewCell

CustomCell.h

#import <UIKit/UIKit.h> 


@interface CustomCell : UITableViewCell { 
    UILabel *mainLabel; 
    UILabel *leftBottomLabel; 
    UILabel *rightBottomLabel; 

} 


@property (nonatomic, retain) UILabel *mainLabel; 
@property (nonatomic, retain) UILabel *leftBottomLabel; 
@property (nonatomic, retain) UILabel *rightBottomLabel; 


@end 

CustomCell.m

#import "CustomCell.h" 


@implementation CustomCell 

@synthesize mainLabel; 
@synthesize leftBottomLabel; 
@synthesize rightBottomLabel; 



-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 


     UIView *myContentView = self.contentView; 



     self.mainLabel.textAlignment = UITextAlignmentLeft; 
     [myContentView addSubview:self.mainLabel]; 
     [self.mainLabel release]; 



     self.leftBottomLabel.textAlignment = UITextAlignmentLeft; 
     [myContentView addSubview:self.leftBottomLabel]; 
     [self.leftBottomLabel release]; 


     self.rightBottomLabel.textAlignment = UITextAlignmentLeft; // default 
     [myContentView addSubview:self.rightBottomLabel]; 
     [self.rightBottomLabel release]; 
    } 


    return self; 
} 


- (void)layoutSubviews { 

    [super layoutSubviews]; 


    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 


     frame = CGRectMake(boundsX + 10, 4, 200, 20); 
     self.mainLabel.frame = frame; 
     self.mainLabel.backgroundColor = [UIColor redColor]; 

     frame = CGRectMake(boundsX + 10, 28, 200, 20); 
     self.leftBottomLabel.frame = frame; 
     self.leftBottomLabel.backgroundColor = [UIColor greenColor]; 


     frame = CGRectMake(boundsX + 100, 28, 200, 14); 
     self.rightBottomLabel.frame = frame; 
     self.rightBottomLabel.backgroundColor = [UIColor blueColor]; 

} 



- (void)dealloc { 

    [mainLabel dealloc]; 
    [leftBottomLabel dealloc]; 
    [rightBottomLabel dealloc]; 
    [super dealloc]; 
} 

@end 

MyView.m

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

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 

    cell.mainLabel.text = @"Hello"; 
return cell; 
} 

任意の考え?私はこのラベルの中に何も得ていない、理由を知らない。

+0

これはおそらくあなたの問題とは関係ありませんが、deallocメソッドではなく、deallocでラベルにreleaseを呼び出す必要があります。 – Joe

+0

OMGあなたは正しいです。ありがとう。 – Clay

答えて

3

3つのラベルをそれぞれ作成してからプロパティを変更したり、スクリーン描画を行う必要があります。現在のところ、これらの3つのラベルは宣言されています。あなたのinitWithStyle:reuseIdentifier:で、修正するには、先頭に次の行を追加します。

self.mainLabel = [[UILabel alloc] init]; 
self.leftBottomLabel = [[UILabel alloc] init]; 
self.rightBottomLabel = [[UILabel alloc] init]; 

'での(自分自身後のフレームを設定すると、あなたが、初期化する[[UILabel alloc] initWithFrame..]を必要といけない理由である)フレームの変更を処理するあなたのlayoutSubviewsコード適切な時間になり、ラベルが正しく表示されるようになります。

+0

OMGありがとうございました...朝の午前中のコードで作業するためのものです。これはうまくいった。 – Clay

+0

この回答が正しい場合は、正しいものとしてマークしてください。 –

+0

完了し、完了しました。ありがとう。 – Clay

1

ラベルを実際に作成するようなことはありません。

あなたのinitコードで

mailLabel = [[UILabel alloc] initwithframe...] 

を必要としています。

関連する問題