簡體   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