以编程方式创建的UITableViewCell子类仅用于突出显示

问题描述 投票:1回答:1

我已经创建了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;
}
iphone uitableview
1个回答
0
投票

为了完整起见,我的问题是尝试使用自定义单元格默认cell.textLabel.text而不是我的自定义cell.primaryLabel.text

只要在单元格中使用其中一个默认对象,它就会恢复为默认单元格而不是子类。

© www.soinside.com 2019 - 2024. All rights reserved.