Программно созданный подкласс UITableViewCell, работающий только с выделением - PullRequest
1 голос
/ 29 ноября 2011

Я создал подкласс UITableViewCell, но я изо всех сил пытаюсь заставить его работать должным образом.Если я использую UITableViewStyleDefault, тогда класс работает только при выделении.Если я использую UITableViewStyleValue1, то это в основном работает, но я не могу изменить шрифты метки.

Я пытался исследовать, но кажется, что все делают это через файл .xib, но не программно.

Файл реализации #import "ASCustomCellWithCount.h"

@implementation ASCustomCellWithCount
@synthesize primaryLabel,secondaryLabel,contentCountImage,contentCount;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
    contentCountImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"tableCount.png"] ];
    primaryLabel = [[UILabel alloc] init];
    primaryLabel.textAlignment = UITextAlignmentLeft;
    primaryLabel.textColor = [UIColor blackColor];
    primaryLabel.font = [UIFont systemFontOfSize: 20];
    primaryLabel.backgroundColor = [UIColor clearColor];

    secondaryLabel = [[UILabel alloc] init];
    secondaryLabel.textAlignment = UITextAlignmentLeft;
    secondaryLabel.textColor = [UIColor blackColor];
    secondaryLabel.font = [UIFont systemFontOfSize: 8];
    secondaryLabel.backgroundColor = [UIColor clearColor];

    contentCount = [[UILabel alloc] init];
    contentCount.textAlignment = UITextAlignmentCenter;
    contentCount.font = [UIFont boldSystemFontOfSize: 15];
    contentCount.textColor = [UIColor whiteColor];
    contentCount.shadowColor = [UIColor blackColor];
    contentCount.shadowOffset = CGSizeMake(1, 1);
    contentCount.backgroundColor = [UIColor clearColor];

    [self.contentView addSubview: contentCountImage];
    [self.contentView addSubview: primaryLabel];
    [self.contentView addSubview: secondaryLabel];
    [self.contentView addSubview: contentCount];
}
return self;
}

- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
//    CGFloat boundsX = contentRect.origin.x;
primaryLabel.frame = CGRectMake(0 ,0, 200, 25);
secondaryLabel.frame = CGRectMake(0, 30, 100, 15);
contentCount.frame = CGRectMake(contentRect.size.width - 48, contentRect.size.height / 2 - 13, 36, 24);
contentCountImage.frame = CGRectMake(contentRect.size.width - 48, contentRect.size.height / 2 - 12, 36, 24);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state
}

- (void)dealloc {
[primaryLabel release];
[secondaryLabel release];
[contentCountImage release];
[contentCount release];
}
@end

И затем, чтобы создать ячейку, я использую

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

ASCustomCellWithCount *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[ASCustomCellWithCount alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [NSString stringWithFormat:@"%@", [tempArray objectAtIndex: indexPath.row]];
cell.contentCount.text = @"49";
    return cell;
}

1 Ответ

0 голосов
/ 05 октября 2012

Ради полноты я пытался использовать пользовательские ячейки по умолчанию cell.textLabel.text вместо моих пользовательских cell.primaryLabel.text.

Как только вы используете один из объектов по умолчанию в ячейке, он возвращается к ячейкам по умолчанию, а не к подклассу.

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