Метка не будет отображать текст после создания подкласса UITableViewCell - PullRequest
2 голосов
/ 24 июля 2011

Итак, я знаю, что об этом спрашивали миллион раз. Может быть, я слишком устал, но я не могу понять, почему этот текст ярлыка не обновится. Вот мой код

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;
}

Есть мысли? На этом ярлыке ничего нет, не знаю почему.

Ответы [ 2 ]

3 голосов
/ 24 июля 2011

Вам необходимо сначала создать каждые три метки, прежде чем вы сможете изменить их свойства или получить изображение экрана. На данный момент эти три метки были только объявлены. Чтобы исправить, в вашем initWithStyle:reuseIdentifier: добавьте следующее:

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

Ваш код layoutSubviews будет обрабатывать изменения фрейма (поэтому вам не нужно [[UILabel alloc] initWithFrame..] инициализировать, поскольку вы сами устанавливаете фрейм позже) 'в соответствующее время, и ваши метки теперь должны отображаться правильно.

1 голос
/ 24 июля 2011

Кажется, что вы никогда не создаете метку.

Вам нужен

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

в вашем коде инициализации.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...