1. 程式人生 > >小坑:UITableView分組後最後一根分割線不顯示

小坑:UITableView分組後最後一根分割線不顯示

如圖用section分隔開後每個section最後的cell的分割線不見了。

分隔的方法是:單獨把一個section作為分隔塊使用,即:section 0,有cell 2;section 1,沒有cell,headView高12;section 2,有cell 2……以此類推。

要解決這個問題,想要分割線顯示的話看來只能自定義,加到Layer上面去。

比如說在分隔section的上下加上分割線:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if
(section % 2 - 1 == 0) { return 12; }else return 0; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView* headView = [UIView new]; headView.backgroundColor = [UIColor colorWithWhite:0.946 alpha:1.000]; //分割線 [headView.layer addSublayer:[CALayer separatorLayerInHeight:0
]]; if ([tableView numberOfSections] != section + 1) { //最後一個section不需要下橫線(下面沒有新section資料了) [headView.layer addSublayer:[CALayer separatorLayerInHeight:12]]; } return headView; }
@implementation CALayer (SeparatorView)

+ (CALayer*)separatorLayerInHeight:(CGFloat)height {
    UIColor
*borderColor = [UIColor colorWithWhite:0.709 alpha:1.000]; CGFloat borderHeight = 0.3f; CALayer *layer = [CALayer layer]; layer.frame = CGRectMake(0, height - borderHeight, SCREEN_WIDTH, borderHeight); layer.backgroundColor = borderColor.CGColor; return layer; } @end

效果: