項目中經(jīng)常要用到標題為我的或者設置等的界面,而這些界面一般用storyboard搭建static的cell比較方便快捷,我們經(jīng)常會遇到以下需求:
- 1.tabbleview在group樣式下,自定義不同section的間距
- 2.為不同section中的cell設置圓角
我的解決辦法如下:
1.設置section的間距
- 調(diào)用代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section ;設置section的header高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section設置section的Footer高度
- 或者調(diào)用代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section ;設置section的header高度;然后直接設置 sectionFooterHeight屬性值;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 10;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.sectionFooterHeight = 8;
}
注意 : 帶有導航控制器的導航欄時,直接設置sectionHeaderHeight屬性值,self.tableView.sectionHeaderHeight=10;self.tableView.sectionFooterHeight = 8;會導致第一個section與導航條的間距和其他間距不同,所以最好通過代理方法- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;來設置sectionHeaderHeight的高度
這種情況下,如果不用代理方法,需要設置tableView的contentInset屬性,來解決
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
self.tableView.sectionFooterHeight = 8;
self.tableView.sectionHeaderHeight = 10;
}
2.設置不同section之間cell的圓角
在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;這個代理方法中作判斷,在相應的cell中的圖層繪制
以下代碼可直接復制粘貼使用
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
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:1.0f].CGColor; // cell填充顏色
layer.strokeColor=[UIColor whiteColor].CGColor; // cell邊框顏色
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 *cellBackView = [[UIView alloc] initWithFrame:bounds];
[cellBackView.layer insertSublayer:layer atIndex:0];
cellBackView.backgroundColor = UIColor.clearColor;
cell.backgroundView = cellBackView;
}
運行效果如下:

上圖中第三個section中的cell是單獨設置的,因為上面的代理方法中把cell的填充色重新繪制成了白色
所以直接改變cell的背景色會不起作用(如圖黃色部分),修改cell的contentView的背景色(如圖綠色部分)又會把圓角給遮擋住


所以我對第三個section的cell作了重新設置,自定義了名為QYMineCell的cell,重寫- (void)setFrame:(CGRect)frame ;方法和設置圓角半徑,并與storyboard對應的cell作關聯(lián)
#import "QYMineCell.h"
@implementation QYMineCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
self.layer.cornerRadius = 5.f;
}
- (void)setFrame:(CGRect)frame {
CGFloat cellMargin = 10;
frame.origin.x = cellMargin;
frame.size.width -= cellMargin * 2;
[super setFrame:frame];
}
@end