分组的UITableView,iOS7圆angular的解决方法

自从iOS7的推出以来,UITableView的分组样式的圆angular被Apple删除( 在这里证实 )。 不过,我确信有聪明的人在那里build立了解决方法。

请帮助我和其他许多iOS程序员, 发布你的解决方法,以获得iOS7的UITableViewStyleGrouped中的单元格的圆angular 。 这将是非常感谢!

只需在代码中添加以下UITableView委托方法。 它真的帮助我像iOS7中的表视图一样的问题。 尝试下面的代码,可能是它也解决了你的问题:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(tintColor)]) { if (tableView == tblProfileDetails) { // self.tableview CGFloat cornerRadius = 5.f; cell.backgroundColor = UIColor.clearColor; CAShapeLayer *layer = [[CAShapeLayer alloc] init]; CGMutablePathRef pathRef = CGPathCreateMutable(); CGRect bounds = CGRectInset(cell.bounds, 5, 0); BOOL addLine = NO; if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); } else if (indexPath.row == 0) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); addLine = YES; } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); } else { CGPathAddRect(pathRef, nil, bounds); addLine = YES; } layer.path = pathRef; CFRelease(pathRef); layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; if (addLine == YES) { CALayer *lineLayer = [[CALayer alloc] init]; CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+5, bounds.size.height-lineHeight, bounds.size.width-5, lineHeight); lineLayer.backgroundColor = tableView.separatorColor.CGColor; [layer addSublayer:lineLayer]; } UIView *testView = [[UIView alloc] initWithFrame:bounds]; [testView.layer insertSublayer:layer atIndex:0]; testView.backgroundColor = UIColor.clearColor; cell.backgroundView = testView; } } } 

考虑到在表格中可以有多个组,一些单元格只在顶部四舍五入,另一些在底部,另一些则不是四舍五入的。 所以我在这里创build了一个非常快速的UITableViewCell的子类(可以真正缩短),方法如下:

一个属性,你可以打电话任何你想要的,我称为上下:

 @property(nonatomic,assign) BOOL top,down; 

执行:

 - (void)layoutSubviews { [super layoutSubviews]; if(self.top && self.down) { self.layer.cornerRadius = 10; self.layer.masksToBounds = YES; return; } if (self.top) { CAShapeLayer *shape = [[CAShapeLayer alloc] init]; shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)].CGPath; self.layer.mask = shape; self.layer.masksToBounds = YES; return; } if (self.down) { CAShapeLayer *shape = [[CAShapeLayer alloc] init]; shape.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)].CGPath; self.layer.mask = shape; self.layer.masksToBounds = YES; } } 

使用很简单,如果你的单元格是组中的第一个,设置TOP = True,如果是最后一个,则DOWN = true,如果单元格是组中唯一的,则TOP和DOWN = true。

如果你想要更多或更less的圆angular,只需将无线电从10更改为另一个值。

就是这样,你可以缩短代码的重复次数,或者在任何情况下都使用CAShapeLayer,无论如何,我希望它有帮助。

罗伯托·弗拉兹(Roberto Ferraz)的答案略有改善。

它为你计算出顶部和底部的单元(假设单元之间没有间隙)。

它也使用从iOS7中删除组的边界的技巧

 @interface CustomTableViewCell : UITableViewCell 

覆盖消息:

 #define radius 10 - (void)layoutSubviews { [super layoutSubviews]; CGRect frame = self.frame; CGFloat top = frame.origin.y; CGFloat bottom = top + frame.size.height; UIRectCorner corners = UIRectCornerAllCorners; CAShapeLayer* mask = [CAShapeLayer layer]; for (UIView* view in self.superview.subviews) { if (view.frame.origin.y + view.frame.size.height == top) { corners = corners & ~(UIRectCornerTopLeft|UIRectCornerTopRight); } else if (view.frame.origin.y == bottom) { corners = corners & ~(UIRectCornerBottomLeft|UIRectCornerBottomRight); } } mask.path = [UIBezierPath bezierPathWithRoundedRect: CGRectMake(0, 0, frame.size.width, frame.size.height) byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)].CGPath; self.layer.mask = mask; self.layer.masksToBounds = YES; } /* iOS 7 workaround to get rid of top and bottom separators */ - (void) addSubview:(UIView*)view { if (view.frame.origin.x > 0 || view.frame.size.width < self.frame.size.width) { [super addSubview:view]; } } 

你可以使用UIImageView并像这样设置圆angular半径:

 CALayer * l = [self.cellBackgroundImage layer]; [l setMasksToBounds:YES]; [l setCornerRadius:5.0]; 

要使用这个,你应该input夸脱核心:

 #import <QuartzCore/QuartzCore.h>