2011-11-28 68 views
1

我已經創建了UITableViewCell的子類,但我努力使其正常工作。如果我使用UITableViewStyleDefault,那麼該類僅在突出顯示時才起作用。如果我使用UITableViewStyleValue1那麼它主要工作,但我無法更改標籤字體太多。以編程方式創建的UITableViewCell子類只能用於突出顯示

我試過研究,但似乎每個人都是通過.xib文件來做這件事,但不是以編程方式。

實現文件 #進口「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

你爲什麼要設置'textLabel'文本而不是'primaryLabel'和'secondaryLabel'標籤'文本?另外,'contentCount'標籤是白色的,推測背景是白色的,這可能是爲什麼它只顯示在高亮區域? – mattjgalloway

+0

不知道爲什麼我沒有改變textLabel,謝謝你發現。至於contentCount,在下面有一個深灰色的圖像視圖(contentCountImage),所以標籤仍然應該顯示出來。 – squarefrog

+0

好吧,我不知道爲什麼,但只是將'textLabel'更改爲'primaryLabel'已使自定義樣式起作用。謝謝。我需要的只是一雙新的眼睛:) – squarefrog

回答

0

爲了完整起見,電池,我的問題是試圖使用自定義單元格默認cell.textLabel.text而不是我的自定義cell.primaryLabel.text

只要您在單元格中使用其中一個默認對象,它就會恢復爲默認單元格而不是子類。

相關問題