所以我知道这被问了一百万次。也许我太累了,但我无法弄清楚为什么这个标签文字不会更新。继承人我的代码
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 :(得分:3)
您需要先创建每个三个标签,然后才能更改其属性或进行任何屏幕绘制。目前,这三个标签仅被宣布。要解决此问题,请在initWithStyle:reuseIdentifier:
中,在顶部添加以下内容:
self.mainLabel = [[UILabel alloc] init];
self.leftBottomLabel = [[UILabel alloc] init];
self.rightBottomLabel = [[UILabel alloc] init];
您的layoutSubviews
代码将处理框架更改(这就是您不需要[[UILabel alloc] initWithFrame..]
初始化的原因,因为您在适当的时候自己设置了框架),现在应该显示您的标签正确。
答案 1 :(得分:1)
您似乎永远不会真正创建标签。
你需要一个
mailLabel = [[UILabel alloc] initwithframe...]
在你的初始化代码中。