iphone - 子类化 UITableViewCell 后标签将不会显示文本

标签 iphone ios uitableview

所以我知道这个问题被问了一百万次。也许我太累了,但我不明白为什么这个标签文本不会更新。这是我的代码

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

自定义单元格.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;
}

有什么想法吗?我在这个标签中什么也没得到,不知道为什么。

最佳答案

您需要先创建每三个标签,然后才能更改它们的属性或进行任何屏幕绘制。目前,这三个标签刚刚被申报。要修复此问题,请在 initWithStyle:reuseIdentifier: 的顶部添加以下内容:

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

您的 layoutSubviews 代码将处理框架更改(这就是为什么您不需要 [[UILabel alloc] initWithFrame..] 进行初始化,因为您自己设置框架稍后)' 在适当的时间,您的标签现在应该正确显示。

关于iphone - 子类化 UITableViewCell 后标签将不会显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6805331/

相关文章:

iphone - 是否有同时适用于 iphone 和 android 应用程序的图表库?

iphone - 另一个类的objective-c调用函数

ios - 如何在 Swift 中以编程方式访问系统颜色?

ios - UITableViewCell 内的 UITableView 在选择时将颜色更改为白色

ios - 单击 UITableView 单元格内的图像

ios - 如何使用界面生成器添加 UITableView 部分

iphone - 表格 View 加载更多单元格?

iphone - UITableView使用CALayer + mask的滚动性能

ios - UICollectionView 自定义单元格在我滚动时自动刷新

ios - UISearchController:用于显示和搜索的单个与单独的表格 View