繁体   English   中英

以编程方式创建的UITableViewCell子类仅在突出显示时起作用

[英]Programmatically created UITableViewCell subclass only working on highlight

我创建了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;
}

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

在单元格中使用默认对象之一后,它将立即恢复为默认单元格,而不是子类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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