我的iPhone应用程序当前正在显示包含2个部分的分组表。我尝试使用以下方法更改每个部分的第一个和最后一个单元格(或者在只有一行的部分的相同单元格上)的圆角半径:
cell.layer.cornerRadius = 4;
或者对整个桌面视图采取行动:
self.tableView.layer.cornerRadius = 4;
但没有运气......当然我在执行此操作之前已经导入了QuartzCore。看起来只有在以简单样式显示表格时才可以自定义角点。
理想的解决方案是在最后一个中定制顶角和底角。
有什么建议?
最简单的方法是:
@"beginCell", @"middleCell", @"endCell"
。您可以根据本教程制作它:http://www.bdunagan.com/2009/06/28/custom-uitableviewcell-from-a-xib-in-interface-builder/- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
中,您必须找到您在该部分的中间或开始/结束处并使用具有适当标识符的单元格。你可以做的是在细胞的背景上添加一个UIView
,使细胞背景为清晰的颜色。调整UIView
有圆角。
这是在Swift 4.2中有效的:
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
let cornerRadius = 5
var corners: UIRectCorner = []
if indexPath.row == 0
{
corners.update(with: .topLeft)
corners.update(with: .topRight)
}
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1
{
corners.update(with: .bottomLeft)
corners.update(with: .bottomRight)
}
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: cell.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: cornerRadius, height: cornerRadius)).cgPath
cell.layer.mask = maskLayer
}
UIView *view = [UIView alloc]initWithFrame:YourFrame];
view.layer.cornerRadius = 8;
cell.backgroundView = view;
并且不要忘记UITableView
背景颜色是透明的。
如果你只需要圆角,那就是另一种解决方案(灵感来自本机uitableview(单元格)的分组样式,在slowmo :))。它应该是这样的:
细胞亚类......
@interface MyCell : UITableViewCell
@property (nonatomic) top;
@property (nonatomic) bottom;
@end
@implementation MyCell
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor]; // remove white default background
//example settings, UIImageViews with resizable images can be used too
self.backgroundView = [[UIView alloc] init];
self.selectedBackgroundView = [[UIView alloc] init];
self.backgroundView.backgroundColor = [UIColor greenColor];
self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
self.backgroundView.layer.cornerRadius = self.selectedBackgroundView.layer.cornerRadius = 5; //
}
return self;
}
-(void)layoutSubviews{
[super layoutSubviews];
self.backgroundView.frame = self.selectedBackgroundView.frame = UIEdgeInsetsInsetRect(self.bounds, UIEdgeInsetsMake(self.top?0:-self.backgroundView.layer.cornerRadius, 0, self.bottom?0:-self.backgroundView.layer.cornerRadius, 0));
}
-(void)setTop:(BOOL)top{
_top = top;
[self setNeedsLayout];
}
-(void)setBottom:(BOOL)bottom{
_bottom = bottom;
[self setNeedsLayout];
}
@end
在dataSource中你可以这样做:...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...cell initialization stuff...
[(MyCell *)cell setTop:(indexPath.row == 0)];
[(MyCell *)cell setBottom:(indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)];
return cell;
}
优点:
缺点:
我希望这对某人有所帮助,但请注意,此代码未经过测试!我在发布这个答案前几分钟实现了这个想法,它似乎工作:)