所以我知道这是被问了一百万次。也许我太累了,但我不明白为什么这个标签文本不会更新。这是我的代码
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) initWithStyleUITableViewCellStyle)style reuseIdentifierNSString *)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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}
有什么想法吗?我在这个标签上什么都没有,不知道为什么。
Best Answer-推荐答案 strong>
您需要先创建每三个标签,然后才能更改它们的属性或进行任何屏幕绘制。目前,这三个标签才被宣布。要修复,在您的 initWithStyle:reuseIdentifier: 中,在顶部添加以下内容:
self.mainLabel = [[UILabel alloc] init];
self.leftBottomLabel = [[UILabel alloc] init];
self.rightBottomLabel = [[UILabel alloc] init];
您的 layoutSubviews 代码将处理框架更改(这就是为什么您不需要 [[UILabel alloc] initWithFrame..] 来初始化,因为您自己设置框架later) ' 在适当的时间,您的标签现在应该可以正确显示了。
关于iphone - 子类化 UITableViewCell 后标签不会显示文本,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/6805331/
|