简体   繁体   中英

Programmatically created UITableViewCell subclass only working on highlight

I've created a subclass of UITableViewCell but I'm struggling to get it to work properly. If I use UITableViewStyleDefault then the class only works when highlighted. If I use UITableViewStyleValue1 then it mostly works but I'm unable to change label fonts much.

I tried researching but it seems everyone is doing this via a .xib file, but not programmatically.

Implementation file #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

And then to create the cell I use

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

For the sake of completeness, my issue was trying to use the custom cells default cell.textLabel.text rather than my custom cell.primaryLabel.text .

As soon as you use one of the default objects in a cell it reverts to the default cells rather than a subclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM