我有表格视图,其中每个单元格都有渐变背景,并且当单元格大小相同时它可以工作。我对另一个数据集和可变单元格大小使用相同的表格视图。如果我将渐变代码放在自定义单元格的 initwithcoder 中,那么它会为原始单元格的大小创建一个渐变。我尝试了不同的东西,但无法获得合适的尺寸。唯一的答案是在 cellForRowAtIndexPath 内部创建一个临时框架,其中单元格的高度来自基于文本计算它的方法。我试图弄清楚为什么 cellForRowAtIndexPath 内的单元格高度不正确。这是使其更清晰的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = @"feedCell";
FeedTableViewCell *cell = (FeedTableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FeedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell.contentView viewWithTag:999]removeFromSuperview];
// Configure the cell...
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:16.0];
cell.signText.font = cellFont;
CGRect lblFrame = cell.signText.frame;
lblFrame.size.height = [self labelHeightForText:[object objectForKey:@"text"]];
cell.signText.frame = lblFrame;
cell.signText.lineBreakMode = NSLineBreakByWordWrapping;
cell.signText.text = [object objectForKey:@"text"];
UIColor *grayLightOp = [UIColor colorWithRed:0.863 green:0.841 blue:0.812 alpha:1.000];
CAGradientLayer *gradient = [CAGradientLayer layer];
//here is the tempFrame that i am talking about i can't just set tempFrame = cell.contentView.frame
CGRect tempFrame = CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, [self labelHeightForText:[object objectForKey:@"text"]]);
gradient.frame = tempFrame;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)grayLightOp.CGColor, nil];
UIView *backgroundView = [[UIView alloc]initWithFrame:gradient.frame];
backgroundView.tag = 999;
[backgroundView.layer insertSublayer:gradient above:0];
[cell.contentView insertSubview:backgroundView atIndex:0];
[cell.backgroundView setNeedsDisplay];
return cell;
}
-(CGFloat)labelHeightForText:(NSString *)text
{
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:16.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
return labelSize.height + 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
PFObject *feedObject = [self objectAtIndexPath:indexPath];
NSString *cellText = [feedObject objectForKey:@"text"];
return [self labelHeightForText:cellText];
}
======更新我在 cellForRowAtIndexPath 中注释掉了渐变的创建,这是来自自定义单元格的代码。我想我必须调整渐变本身而不是背景视图的大小。当我这样做时,渐变大小是正确的,但是当我滚动时,我会看到当单元格出现时正在绘制渐变的快速动画。我能做点什么吗?
@interface FeedTableViewCell()
@property (nonatomic, strong)CAGradientLayer *cellGradient;
@end
@implementation FeedTableViewCell
@synthesize signText = _signText;
@synthesize personName = _personName;
@synthesize personImageView = _personImageView;
@synthesize cellGradient = _cellGradient;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UIColor *grayLightOp = [UIColor colorWithRed:0.863 green:0.841 blue:0.812 alpha:1.000];
self.cellGradient = [CAGradientLayer layer];
self.cellGradient.frame = self.contentView.frame;
self.cellGradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)grayLightOp.CGColor, nil];
UIView *backgroundView = [[UIView alloc]initWithFrame:self.cellGradient.frame];
backgroundView.tag = 998;
[backgroundView.layer insertSublayer:self.cellGradient above:0];
self.backgroundView = backgroundView;
//[self.layer insertSublayer:gradient atIndex:0];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.cellGradient.frame = self.contentView.frame;
[CATransaction commit];
}
// 用没有背景视图的新代码更新
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
UIColor *grayLightOp = [UIColor colorWithRed:0.863 green:0.841 blue:0.812 alpha:1.000];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.frame;
gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor, (id)grayLightOp.CGColor, nil];
/*UIView *backgroundView = [[UIView alloc]initWithFrame:gradient.frame];
[backgroundView.layer insertSublayer:gradient above:0];
self.backgroundView = backgroundView;
[backgroundView.layer insertSublayer:gradient atIndex:0];*/
[self.layer insertSublayer:gradient atIndex:0];
}
return self;
}