在iOS7中,我如何在UITableView中绘制单元格的圆角?请参考以下示例:
你的 UITableview
包含了 UIView
,所以只需要使用以下代码来使它变成圆角。同时,在你的 tableview 方法中加入以下代码:
//如果 iOS 版本 < 10
对于 Objective-C:
TBD
cell.contentView.layer.cornerRadius = 5;
cell.contentView.layer.masksToBounds = YES;
对于Swift:
cell.contentView.layer.cornerRadius = 5
cell.contentView.layer.masksToBounds = true
//如果iOS版本 >= 10
对于Objective-C:
cell.layer.cornerRadius = 5;
cell.layer.masksToBounds = YES;
对于Swift:
cell.layer.cornerRadius = 5
cell.layer.masksToBounds = true
注意:无需显式导入QuartzCore框架
。
YES
è،¨ç¤؛Objective-C,ن½؟用true
è،¨ç¤؛Swiftم€‚ - KingPolygon我创建了UITableViewCell的子类,为了让它正常工作必须省略contentView。
cell.layer.cornerRadius = 10
cell.layer.maskToBounds = true
cell.backgroundColor
或非透明的cell.backgroundView
,那么cell.contentview.layer.cornerRadius
将不足以实现效果。这就是为什么hidden-username解决方案可能比Hussain Shabbir或Fahim Parkar更好的原因。 - Cœur[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;
}
注意/建议: 最好使用单元格 XIB/NIB 文件,在其中添加一个 UIView,通过约束保持 上、下、左、右 四个角的间距,将单元格背景设为透明并将 UIView 圆角化。我的解决方案可以解决这种情况。但是始终要遵循最佳实践。