我有一个UITableView,其中包含具有以下实现的单元格:
static NSString *AttractionCellIdentifier = @"AttractionCell";
AttractionCellTableViewCell *cell = (AttractionCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AttractionCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AttractionCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.gradientView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor]CGColor], nil];
[cell.gradientView.layer insertSublayer:gradient atIndex:0];
cell.gradientView.alpha = 0.8;
}
cell.backgroundPhoto.image = [UIImage imageWithData:photosArray[indexPath.row]];
cell.backgroundPhoto.contentMode = UIViewContentModeScaleAspectFill;
[cell.backgroundPhoto setClipsToBounds:YES];
NSDictionary *morning = [[NSDictionary alloc]initWithDictionary:random[indexPath.row]];
cell.attractionName.numberOfLines = 2;
cell.attractionName.text = [NSString stringWithFormat:@"%@",morning[@"name"]];
[cell.attractionName sizeToFit];
CGRect attractionNameRect = [cell.attractionName frame];
attractionNameRect.origin.x = (self.view.frame.size.width/2) - (attractionNameRect.size.width/2);
[cell.attractionName setFrame:attractionNameRect];
[cell.attractionName setNeedsLayout];
cell.borderView.layer.borderWidth = 2.5f;
cell.borderView.layer.borderColor = [UIColor whiteColor].CGColor;
CGRect borderViewRect = [cell.borderView frame];
borderViewRect.size.width = cell.attractionName.frame.size.width+20;
borderViewRect.size.height = cell.attractionName.frame.size.height+20;
[cell.borderView setFrame:borderViewRect];
borderViewRect.origin.x = cell.attractionName.frame.origin.x-10;
borderViewRect.origin.y = cell.attractionName.frame.origin.y-10;
[cell.borderView setFrame:borderViewRect];
[cell.borderView setNeedsLayout];
这将处理下面屏幕截图中白色文本中显示的attractionName标签,以及在attractionName标签周围显示的白色边框borderView。
重新加载表时,它看起来像这样:
当我第一次向下滚动时,第一个问题发生在第三个单元格中:
出于某种原因,第三个单元格在第一次滚动时从不更新borderView,并从.xib文件中显示具有视图原始尺寸的边框。一旦我向上滚动然后再向下滚动到第三个单元格,borderView才会正确显示。
当我在桌子上上下滚动几次(隐藏并显示第一个单元格)时,会出现第二个问题:
最后:
吸引力名称的宽度逐渐减少,名称被切断,我真的无法理解为什么。我曾尝试在使用sizeToFit之前将每个标签的宽度设置为原始宽度,但这会导致sizeToFit对标签的尺寸没有影响。如果有人能提出解决方案,那就太好了。)