開發中有遇到要把長方形的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;
至此完工。
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。