重写willDisplayCell方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[(LQTableViewCell *)cell handleWillDisplayCellWithIndexPath:indexPath numberOfRows:[tableView numberOfRowsInSection:indexPath.section]];
}
LQTableViewCell通过UIBezierPath实现圆角流程
@interface LQTableViewCell ()
@end
@implementation LQTableViewCell
- (void)handleWillDisplayCellWithIndexPath:(NSIndexPath *)indexPath
numberOfRows:(NSInteger)count
CGRect bounds = CGRectInset(self.bounds, 10, - 1)
/*
为了切割掉多余的投影
将顶部和底部的backgroundView进行一些偏移,并且打开裁剪
*/
if (count > 1) {
if (indexPath.row == 0) {
bounds = CGRectMake(10, 3, self.bounds.size.width - 20, self.bounds.size.height + 3)
} else if (indexPath.row == count - 1) {
bounds = CGRectMake(10, -5, self.bounds.size.width - 20, self.bounds.size.height + 3)
}
}
UIView *backgroundView = [[UIView alloc] initWithFrame:bounds]
if (count > 1) {
backgroundView.clipsToBounds = YES
}
self.backgroundView = backgroundView
/*
将背景色都设置为透明色,以方便自定义
*/
backgroundView.backgroundColor =
self.backgroundColor =
self.contentView.backgroundColor = UIColor.clearColor
CAShapeLayer *layer = [[CAShapeLayer alloc] init]
layer.shadowColor = [UIColor cyanColor].CGColor
layer.shadowOffset = CGSizeZero
layer.shadowOpacity = 0.5
layer.fillColor = [UIColor whiteColor].CGColor
UIBezierPath*path = [UIBezierPath bezierPathWithRect:bounds]
CGSize cornerRadii = CGSizeZero
UIRectCorner corners = UIRectCornerAllCorners
/*
1.当数量大于1时
1.1 第一行实现顶部圆角,最后一行实现底部圆角,中间部分不加圆角
1.2 当不是第一行的时候,添加分割线
2.当数量为1时(只有一行)
2.1实现全圆角
*/
if (count > 1) {
if (indexPath.row == 0) {
cornerRadii = CGSizeMake(10, 10)
corners = UIRectCornerTopLeft | UIRectCornerTopRight
} else if(indexPath.row == count - 1) {
cornerRadii = CGSizeMake(10, 10)
corners = UIRectCornerBottomLeft | UIRectCornerBottomRight
}
if (corners != UIRectCornerAllCorners) {
path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:corners cornerRadii:cornerRadii]
} else {
path = [UIBezierPath bezierPathWithRect:bounds]
}
if (indexPath.row != 0) {
CALayer *border=[[CALayer alloc] init]
border.frame = CGRectMake(CGRectGetMinX(bounds),0, CGRectGetWidth(bounds), 1/[UIScreen mainScreen].scale)
border.backgroundColor=[UIColor separatorColor].CGColor
[layer addSublayer:border]
}
}else {
cornerRadii = CGSizeMake(10, 10)
path =[UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:cornerRadii]
}
layer.path = path.CGPath
[backgroundView.layer insertSublayer:layer atIndex:0]
}
@end
效果图
