iOS开发之UITableviewCell实现两个圆角
2015-12-31 12:26
375 查看
坚持 成长 每日一篇
tableView时常有一个效果,就是当cell在section的第一个位置或者最后一行时,第一个cell左上角和右上角显示圆角,最后一个cell左下角和右下角显示圆角。
如下面代码创建一个cell的子类
CornerTableViewCell.h
CornerTableViewCell.m
实现table的dataSource
tableView时常有一个效果,就是当cell在section的第一个位置或者最后一行时,第一个cell左上角和右上角显示圆角,最后一个cell左下角和右下角显示圆角。
方法一
才用方法一可以实现圆角效果,但是如果table设置了滑动删除的话,第一行和最后一行滑动时候看不到删除按钮如下面代码创建一个cell的子类
CornerTableViewCell.h
#import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger,RoundCornerType) { KKRoundCornerCellTypeTop, KKRoundCornerCellTypeBottom, KKRoundCornerCellTypeSingleRow, KKRoundCornerCellTypeDefault }; @interface CornerTableViewCell : UITableViewCell { CGFloat _cornerRadius; } @property(nonatomic,readwrite,assign)RoundCornerType roundCornerType; -(id)initWithCornerRadius:(CGFloat)radius Style:(UITableViewCellStyle)tyle reuseIdentifier:(NSString*)identifier; @end
CornerTableViewCell.m
#import "CornerTableViewCell.h" @implementation CornerTableViewCell -(id)initWithCornerRadius:(CGFloat)radius Style:(UITableViewCellStyle)tyle reuseIdentifier:(NSString*)identifier { self = [super initWithStyle:tyle reuseIdentifier:identifier]; if (self) { _cornerRadius = radius; } return self; } -(void)setFrame:(CGRect)frame { [super setFrame:frame]; UIBezierPath *path; switch (_roundCornerType) { case KKRoundCornerCellTypeTop: { path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)]; break; } case KKRoundCornerCellTypeBottom: { path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)]; break; } case KKRoundCornerCellTypeSingleRow: { path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)]; break; } case KKRoundCornerCellTypeDefault: default: { self.layer.mask = nil; return; } } CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.bounds; maskLayer.path = path.CGPath; self.layer.mask = maskLayer; } @end
实现table的dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *kCellID = @"CELLID"; CornerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; if (!cell) { cell = [[CornerTableViewCell alloc] initWithCornerRadius:10.0f Style:UITableViewCellStyleDefault reuseIdentifier:kCellID]; } if ([tableView numberOfRowsInSection:indexPath.section] == 1) { cell.roundCornerType = KKRoundCornerCellTypeSingleRow; } else if (indexPath.row == 0) { cell.roundCornerType = KKRoundCornerCellTypeTop; } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) { cell.roundCornerType = KKRoundCornerCellTypeBottom; } else { cell.roundCornerType = KKRoundCornerCellTypeDefault; } cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0]; cell.textLabel.text = [NSString stringWithFormat:@"第%ld组第%ld行", indexPath.section + 1, indexPath.row + 1]; return cell; }
方法二
方法二实现圆角没有方法一所说的问题,方法二通过给backGroudView添加mask来实现圆角效果- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(tintColor)]) { if (tableView == self.tableView) { CGFloat cornerRadius = 5.f; cell.backgroundColor = UIColor.clearColor; CAShapeLayer *layer = [[CAShapeLayer alloc] init]; CGMutablePathRef pathRef = CGPathCreateMutable(); CGRect bounds = CGRectInset(cell.bounds, 10, 0); BOOL addLine = NO; if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius); } else if (indexPath.row == 0) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds)); addLine = YES; } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) { CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds)); CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius); CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius); CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds)); } else { CGPathAddRect(pathRef, nil, bounds); addLine = YES; } layer.path = pathRef; CFRelease(pathRef); layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor; if (addLine == YES) { CALayer *lineLayer = [[CALayer alloc] init]; CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale); lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight); lineLayer.backgroundColor = tableView.separatorColor.CGColor; [layer addSublayer:lineLayer]; } UIView *testView = [[UIView alloc] initWithFrame:bounds]; [testView.layer insertSublayer:layer atIndex:0]; testView.backgroundColor = UIColor.clearColor; cell.backgroundView = testView; } } }
相关文章推荐
- 类似通讯录的列表
- 针对NGUI点击穿透UI,场景响应的问题
- Description The project was not built due to "Could not delete
- UIScrollView在有点击手势的情况下,会导致UITableView的cell点击事件无法响应
- iOS开发 UICollectionView详解+实例
- 【日常】摸一摸怎样把LOL修改成英文版
- UUId与keychain
- Java线程(篇外篇):阻塞队列BlockingQueue
- UVa1608 UVaLive6258 Non-boring sequences
- UITableViewCell的选中时的颜色设置
- Selenium获取input值的两种方法:WebElement.getAttribute("value")和WebElement.getText()
- 重写UIView
- WeUI首页、文档和下载 - 专为微信设计的 UI 库 - 开源中国社区
- UICollectionViewFlowLayout has cached frame mismatch for index path <NSIndexPath: 0xc000000001400016
- iOS开发UI篇章 15-项目中的常见文件
- layer设置UIImageView圆形不圆的问题
- iOS日期选择器(UIDatePicker)
- iOS中UIImage存为本地文件,UIImage转换为NSData
- Flash builder4.5免注册
- iOS内存优化——OperationQueue悄悄给你挖的坑