之前有人問我這個問題舶得,如何為tableview的每一個分組添加一個圓角呀舔。就像下圖的卡片效果,其實(shí)很簡單扩灯。我們只需要把每一組的第一個cell和最后一個cell切掉兩個角就好了媚赖。開始前請將cell的背景色設(shè)置透明,contentView的背景色設(shè)置為你想要的顏色珠插。代碼在下面惧磺。。捻撑。
-(void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
if(indexPath.section!=1&& indexPath.row==0) {
? ? ? ? UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
? ? ? ? //創(chuàng)建 layer
? ? ? ? CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
? ? ? ? maskLayer.frame= cell.contentView.bounds;
? ? ? ? //賦值
? ? ? ? maskLayer.path= maskPath.CGPath;
? ? ? ? cell.contentView.layer.mask= maskLayer;
? ? }
? ? if((indexPath.section==0&& indexPath.row==5)||(indexPath.section==1&& indexPath.row==0)||(indexPath.section==2&& indexPath.row==2)){
? ? ? ? UIBezierPath*maskPath;
//由于我的第二組只有一個cell 磨隘,所以這里分開設(shè)置缤底。將第二組四個角全切
? ? ? ? if(indexPath.row==0) {
? ? ? ? ? ? maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(10, 10)];
? ? ? ? }else{
? ? ? ? ? ? maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.contentView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
? ? ? ? }
? ? ? ? //創(chuàng)建 layer
? ? ? ? CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
? ? ? ? maskLayer.frame= cell.contentView.bounds;
? ? ? ? //賦值
? ? ? ? maskLayer.path= maskPath.CGPath;
? ? ? ? cell.contentView.layer.mask= maskLayer;
? ? }
? ?}