我制作了定制的uiTableViewCell,并像截图一样使用UITableview。问题是groupd表中第一个和最后一个单元格的圆角。
如何使第一个和最后一个单元格成为圆角
发布于 2014-01-16 18:48:06
将tableView作为属性添加到您的单元格,并在控制器中设置它:
@property (nonatomic, weak) UITableView *tableView;
然后,您可以像这样实现它:
- (void)layoutSubviews {
[super layoutSubviews];
UIRectCorner rectCorner;
BOOL roundCorners = YES;
NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
NSInteger numberOfRows = [self.tableView numberOfRowsInSection:indexPath.section];
if (numberOfRows == 1) { // single cell
rectCorner = UIRectCornerAllCorners;
} else if (indexPath.row == numberOfRows - 1) { // bottom cell
rectCorner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
} if (indexPath.row == 0) { // top cell
rectCorner = UIRectCornerTopLeft | UIRectCornerTopRight;
} else {
roundCorners = NO;
}
if (roundCorners) {
CGFloat cornerRadius = 10.0;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:rectCorner
cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
}
}
你可以通过检查self.superview.superview
(iOS7,之前的self.superview
)来避免在属性上使用tableView
,但我认为这是一种更干净、更不容易出错的方法。
发布于 2011-10-31 22:51:44
诀窍是设置
view.layer.cornerRadius = 10;
请看this link! 上的这个示例项目。
发布于 2012-10-31 16:13:35
尝试设置单元格的背景颜色clearColor:您的表格似乎有圆角,但您的单元格正在覆盖它。另一种替代解决方案可能是更改表后面视图的属性。尝试:
backgroundView.clipsToBounds = YES;
或
tableView.layer.cornerRadius = 5;
tableView.layer.masksToBounds = YES;
您可能需要
#import <QuartzCore/QuartzCore.h>
https://stackoverflow.com/questions/7955206
复制相似问题