开发中有遇到要把长方形的cell改为圆角的,网上查了资料发现好多网友在如下方法中重绘cell。具体代码大家可以自行百度。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}
然后自己思考有没有别的方法可以实现。于是就尝试了一下。自己重写了如下方法。发现效果还不错。
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{}
如图:
废话不多说直接上代码
.h中声明方法
#import <UIKit/UIKit.h>
@interface HanTableViewCell : UITableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellForRowAtIndexPath:(NSIndexPath *)indexPath withTabel:(UITableView *)tableView;
@end
.m 中实现
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellForRowAtIndexPath:(NSIndexPath *)indexPath withTabel:(UITableView *)tableView
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
bgView.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:bgView];
UIBezierPath *path = [UIBezierPath bezierPath];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
maskLayer.frame = bgView.bounds;
bgView.layer.mask = maskLayer;
maskLayer.fillColor = [UIColor whiteColor].CGColor;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
{
path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds cornerRadius:22];
}
else if (indexPath.row == 0)
{
path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeLeft|UIRectEdgeTop cornerRadii:CGSizeMake(22, 22)];
}
else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1)
{
path = [UIBezierPath bezierPathWithRoundedRect:bgView.bounds byRoundingCorners:UIRectEdgeRight|UIRectEdgeBottom cornerRadii:CGSizeMake(22, 22)];
}
else
{
path = [UIBezierPath bezierPathWithRect:bgView.bounds];
}
maskLayer.path = path.CGPath;
[bgView.layer addSublayer:maskLayer];
if (indexPath.row != [tableView numberOfRowsInSection:indexPath.section]-1 )
{
CALayer *lineLayer = [CALayer layer];
lineLayer.borderWidth = 1;
lineLayer.borderColor = tableView.separatorColor.CGColor;
lineLayer.frame = CGRectMake(20, 43, tableView.frame.size.width - 40, 1);
[bgView.layer addSublayer:lineLayer];
}
}
return self;
}
具体使用:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellStr = @"cell";
HanTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!cell)
{
cell = [[HanTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellStr cellForRowAtIndexPath:indexPath withTabel:tableView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (indexPath.row == 0)
{
cell.imageView.image = [UIImage imageNamed:@"l.jpeg"];
}
cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
return cell;
}
重写的方法多传了两个参数:tableView、indexPath 用来区分自定义的cell。
注:由于自定义了cell的分割线,需要把自带的分割线去掉。
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
至此完工。