開發(fā)中有遇到要把長方形的cell改為圓角的,網(wǎng)上查了資料發(fā)現(xiàn)好多網(wǎng)友在如下方法中重繪cell帘睦。具體代碼大家可以自行百度喉磁。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{}
然后自己思考有沒有別的方法可以實(shí)現(xiàn)。于是就嘗試了一下官脓。自己重寫了如下方法协怒。發(fā)現(xiàn)效果還不錯(cuò)。
- (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 中實(shí)現(xiàn)
- (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;
}
重寫的方法多傳了兩個(gè)參數(shù):tableView卑笨、indexPath 用來區(qū)分自定義的cell孕暇。
注:由于自定義了cell的分割線,需要把自帶的分割線去掉赤兴。
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
至此完工妖滔。
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者