【发布时间】:2014-03-29 17:51:00
【问题描述】:
在 iOS7 中,如何在 UITableView 中绘制单元格的圆角?看一个例子:
【问题讨论】:
-
这些单元格可能是自定义绘制的,或者它们正在使用图像。 cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
标签: objective-c uitableview ios7 rounded-corners
在 iOS7 中,如何在 UITableView 中绘制单元格的圆角?看一个例子:
【问题讨论】:
标签: objective-c uitableview ios7 rounded-corners
您的UITableview 包含UIView,因此只需使用以下代码行将其设为圆角即可。
还要在您的 tableview 方法中编写下面的代码行
//如果iOS版本
对于 Objective-C:
cell.contentView.layer.cornerRadius = 5;
cell.contentView.layer.masksToBounds = YES;
对于斯威夫特:
cell.contentView.layer.cornerRadius = 5
cell.contentView.layer.masksToBounds = true
//如果iOS版本>= 10
对于 Objective-C:
cell.layer.cornerRadius = 5;
cell.layer.masksToBounds = YES;
对于斯威夫特:
cell.layer.cornerRadius = 5
cell.layer.masksToBounds = true
注意:无需显式导入QuartzCore framework。
【讨论】:
YES,对于swift 使用true。
我对 UITableViewCell 进行了子类化,并且不得不省略 contentView 以使其正常工作。
cell.layer.cornerRadius = 10
cell.layer.maskToBounds = true
【讨论】:
cell.contentview.layer.cornerRadius 如果单元格有不明确的cell.backgroundColor 或不明确的cell.backgroundView 是不够的。这就是为什么隐藏用户名解决方案可能比 Hussain Shabbir 或 Fahim Parkar 更好的解决方案。
在下面使用...
[cell.contentView.layer setCornerRadius:7.0f];
[cell.contentView.layer setMasksToBounds:YES];
【讨论】:
尝试表视图委托
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.layer.cornerRadius = 10;
cell.layer.masksToBounds = YES;
}
注意/推荐:最好使用 Cell XIB/NIB 文件,添加 UIView 并保持 top、bottom、left &通过约束来右角边距,保持单元格背景透明和圆形UIView角。我的解决方案很适合这种情况。但始终追求最佳实践。
【讨论】: