2011-04-09 87 views
0

以編程方式創建自定義UITableViewCell,它使用以下代碼。我想知道如何使它的背景透明?如何將值從TableView控制器傳遞給customCell類

爲什麼我要它透明的原因是,UITableViewCell的高度是動態的,我不知道如何將「動態高度」代碼傳遞給customCell類,所以我想我會設置一個300左右的最大高度爲CGRect並使背景透明,因此它不會重疊以下單元格。

UPDATE:

使標籤透明不工作,所以基本上我需要的TableViewCell高度傳遞到自定義類,我是一個總的小白所以裸陪我笑

customCell類

#import "customCell.h" 


@implementation customCell 
@synthesize primaryLabel; 
@synthesize secondaryLabel; 
@synthesize priceLabel; 

@synthesize rectColor; 

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self == [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:10]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:8]; 
     priceLabel = [[UILabel alloc]init]; 
     priceLabel.textAlignment = UITextAlignmentLeft; 
     priceLabel.font = [UIFont systemFontOfSize:10]; 

     //self.rectColor = kDefaultRectColor; 

     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:priceLabel]; 

    } 
    return self; 
} 


- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGRect contentRect = self.contentView.bounds; 
    CGFloat boundsX = contentRect.origin.x; 
    CGRect frame; 
    //CGContextSetFillColor(context, [UIColor clearColor]); 


    frame= CGRectMake(boundsX+5 ,5, 200, 15); 
    primaryLabel.frame = frame; 

    frame= CGRectMake(boundsX+10 ,20, 300, 15); 
    secondaryLabel.frame = frame; 

    frame= CGRectMake(boundsX+270 ,0, 50, 15); 
    priceLabel.frame = frame; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

的UITableView類

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     //cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.primaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    cell.primaryLabel.font = [UIFont boldSystemFontOfSize:18]; 
    cell.primaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0); 
    cell.secondaryLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"description"]; 
    cell.secondaryLabel.font = [UIFont systemFontOfSize:14]; 
    cell.secondaryLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0); 
    cell.priceLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"price"]; 

    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *titleString = [[data objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    NSString *detailString = [[data objectAtIndex:indexPath.row] objectForKey:@"description"]; 
    CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 
    CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; 

    return detailSize.height+titleSize.height; 
} 
+0

你的意思是一些細胞有不同的高度,但他們從不改變_or_你想讓一個細胞的高度改變嗎? – deanWombourne 2011-04-09 16:59:27

回答

0

設置的backgroundColor的細胞「[的UIColor clearColor]」

+0

cell.secondaryLabel.backgroundColor = [UIColor clearColor]; - 添加明確,但仍然與其他單元重疊(仍然)。基本上我得到如何將動態高度傳遞到customCell類 – Andyy 2011-04-09 16:35:30

1

如果你讓你的表格單元格大規模的和透明的,你會作出渲染飛馳慢!

我會看看delegate protocol - 這種方法是你如何告訴細胞應該有多高。

調用[tabelView reloadData]將導致爲每個單元調用此方法。把你的動態高度代碼放在那裏。

希望有所幫助。

+1

是的,我發現如果我改變了 – Andyy 2011-04-09 16:33:22

+0

他是正確的忘記使用透明度解決方法,將殺死性能.... – 2011-04-09 16:40:28

+0

我總共noob,你是否能夠更具體還是給我一些關鍵詞來看看? tyvm :) – Andyy 2011-04-09 16:43:11

0

因爲你行高度依賴於您的自定義單元佈局(基於標籤),我會把上漿邏輯的自定義單元格類中(在layoutSubviews),並且在tableViewController做到以下幾點:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // initialize and configure your cell 
    [cell setNeedsLayout]; 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 
    return cell.bounds.size.height; 
} 

我同意其他人的觀點,如果可能的話應該避免使用透明細胞。

相關問題