所以我知道這是被問了一百萬次。也許我太累了,但我不知道爲什麼這個標籤文本不會更新。我的繼承人代碼標籤將不會顯示文本後繼承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;
}
有什麼想法?我在這個標籤裏什麼都沒得到,不知道爲什麼。
這可能與您的問題無關,但您應該在dealloc方法中的標籤上調用release,而不是dealloc。 – Joe
OMG你是對的。謝謝。 – Clay