我有这种方法可以在UITableViewCell的子类内部对角进行舍入,但是无论何时调用它都会产生奇怪的行为,导致第一次没有正确调整大小的标签然后尺寸正确但第二次使用可怕的边框它显示:
-(UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius WithBorder:(BOOL)showBorder
{
if (tl || tr || bl || br) {
UIRectCorner corner = 0; //holds the corner
//Determine which corner(s) should be changed
if (tl) {
corner = corner | UIRectCornerTopLeft;
}
if (tr) {
corner = corner | UIRectCornerTopRight;
}
if (bl) {
corner = corner | UIRectCornerBottomLeft;
}
if (br) {
corner = corner | UIRectCornerBottomRight;
}
UIView *roundedView = view;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = roundedView.bounds;
maskLayer.path = maskPath.CGPath;
roundedView.layer.mask = maskLayer;
if(showBorder){
CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = roundedView.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 4.0f;
borderLayer.strokeColor = [UIColor whiteColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
[roundedView.layer addSublayer:borderLayer];
}
return roundedView;
} else {
return view;
}
}
这会在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中调用,如下所示:
TimeOfDayCell * c = [tableView dequeueReusableCellWithIdentifier:@"weekdaySelectorOn"];
c.day.text = [self.weekdays objectAtIndex:[self weekdayNormalizedIndexForIndexPath:indexPath]];
c.day = (UILabel *)[c roundCornersOnView:c.day onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:12.0 WithBorder:NO];
c.time1 = (UILabel *)[c roundCornersOnView:c.time1 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
c.time2 = (UILabel *)[c roundCornersOnView:c.time2 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
c.time3 = (UILabel *)[c roundCornersOnView:c.time3 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
这产生以下奇怪的输出,第一张图片是第一次显示单元格,第二张图片是第二次显示单元格时:
答案 0 :(得分:6)
所以,事实证明我不仅需要将其放入-(void)layoutSubviews
我还需要明确告诉contentView
layoutIfNeeded
,但即使这还不够,我不得不还要添加setNeedsLayout
UITableViewCell
中的完整解决方案:
-(void)layoutSubviews
{
[super layoutSubviews];
[self.contentView setNeedsLayout];
[self.contentView layoutIfNeeded];
self.day = (UILabel *)[self roundCornersOnView:self.day onTopLeft:YES topRight:YES bottomLeft:NO bottomRight:NO radius:12.0 WithBorder:NO];
self.time1 = (UILabel *)[self roundCornersOnView:self.time1 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
self.time2 = (UILabel *)[self roundCornersOnView:self.time2 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
self.time3 = (UILabel *)[self roundCornersOnView:self.time3 onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:YES radius:12.0 WithBorder:YES];
}
答案 1 :(得分:1)
第一次调用roundCornersOnView:
时看起来单元格的框架是错误的。首先我会尝试将此调用移至tableView:willDisplayCell:forRowAtIndexPath:
如果这不起作用,您应该考虑确保{当单元格更新其帧时,{1}}正在更新其帧。关于here.