[英]Custom rounded corners for first and last cell in a grouped UITableView
我的 iPhone 应用程序当前正在显示一个包含 2 个部分的分组表。 我尝试使用以下方法更改每个部分的第一个和最后一个单元格(或者在只有一行的部分的情况下在同一单元格上)的圆角半径:
cell.layer.cornerRadius = 4;
或作用于整个表视图:
self.tableView.layer.cornerRadius = 4;
但是没有运气......当然我在执行这个操作之前已经导入了 QuartzCore 。 看起来只有当表格以普通样式显示时才能自定义角。
理想的解决方案是自定义第一个角的顶角和最后一个角的底角。
有什么建议?
最简单的方法是:
在xib文件中创建自定义单元格,设置其唯一标识符,例如: @"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
,使单元格背景为clearcolor。 调整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(cell)的分组样式,在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;
}
优点:
缺点:
希望对您有所帮助,但请注意,此代码未经测试! 在发布此答案的几分钟前,我已经实现了这个想法,它似乎很有效:)
我将我的 tableview 单元格子类化:
class CGStandardCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
}
func setCornerRadius(forIndexPath indexPath: IndexPath, inTableView tableView: UITableView) {
let lastRow = tableView.numberOfRows(inSection: indexPath.section) - 1
if indexPath.row == 0 && indexPath.row == lastRow {
self.cornerRadius = 20
self.layer.maskedCorners = [.layerMaxXMinYCorner,
.layerMinXMinYCorner,
.layerMinXMaxYCorner,
.layerMaxXMaxYCorner]
} else if indexPath.row == 0 {
self.cornerRadius = 20
self.layer.maskedCorners = [.layerMaxXMinYCorner,
.layerMinXMinYCorner]
} else if indexPath.row == lastRow {
self.cornerRadius = 20
self.layer.maskedCorners = [.layerMinXMaxYCorner,
.layerMaxXMaxYCorner]
}
}
}
然后在 tableView(_tableView: UITableView, cellForRowAt indexPath: IndexPath) 中调用
cell.setCornerRadius(forIndexPath: indexPath, inTableView: tableView)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.