我以编程方式创建自定义UITableViewCell,它使用以下代码。我想知道如何使它的背景透明?
我希望它透明的原因是UITableViewCell的高度是动态的,我无法弄清楚如何将“动态高度”代码传递给customCell类,所以我想我设置的最大高度为300左右CGRect并使背景透明,因此它不会与以下单元格重叠。
更新
使标签透明不起作用,所以基本上我需要将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 :(得分:1)
如果你让你的桌子细胞变得庞大而透明,那么你的渲染速度会非常慢!
我会看一下delegate protocol - 这个方法就是告诉单元格它应该有多高。
调用[tabelView reloadData]
将导致为每个单元调用此方法。将动态高度代码放在那里。
希望有所帮助。
答案 1 :(得分:0)
将单元格上的backgroundColor设置为“[UIColor clearColor]”
答案 2 :(得分: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;
}
我同意其他人的意见,如果可能的话,应该避免使用透明细胞。