以編程方式創建自定義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;
}
你的意思是一些細胞有不同的高度,但他們從不改變_or_你想讓一個細胞的高度改變嗎? – deanWombourne 2011-04-09 16:59:27