去掉UITableView多餘的空白行分割線

1、問題描述

在學習和開發中常常會遇到下面的問題,UITableView的UITableViewCell不多或者沒有時,但UITableView有不少的空白行分割線。以下圖:學習

 

如何去掉UITableView多餘的空白行分割線?spa

2、問題分析

方法一:隱藏UITableViewCell自帶的分割線,而後自定義分割線到UITableViewCell。自定義分割線的方法有不少種,能夠自行查找。code

方法二:很簡單,修改tableFooterView。建立frame爲CGRectZero的UIView,賦值給tableFooterView對象

 

2、問題解決

1.自定義分割線

列舉自定義分割線的其中一種方法。blog

步驟一:全局設置UITableViewCell系統自帶分割線「隱藏」,這個「隱藏」只是把分割線顏色設置爲透明。這樣作目的是爲了保持自定義分割線frame和系統自帶的分割線同樣。若是不想同樣,能夠真正隱藏。開發

1 -(void)viewDidLoad
2 {
3     //設置分割線的風格
4     self.tableViewCategory.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
5     self.tableViewCategory.separatorColor = [UIColor clearColor];
6     self.tableViewList.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
7     self.tableViewList.separatorColor = [UIColor clearColor];
8 }

 

步驟二:在UITableViewCellit

 1 // 自繪分割線
 2 - (void)drawRect:(CGRect)rect
 3 {
 4     //獲取cell系統自帶的分割線,獲取分割線對象目的是爲了保持自定義分割線frame和系統自帶的分割線同樣。若是不想同樣,能夠忽略。
 5     UIView *separatorView = [self valueForKey:@"_separatorView"];
 6     NSLog(@"%@",NSStringFromCGRect(separatorView.frame));
 7     NSLog(@"%@",NSStringFromCGRect(rect));
 8     [super drawRect:rect];
 9     CGContextRef context = UIGraphicsGetCurrentContext();
10     CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1].CGColor);
11     //CGContextStrokeRect(context, CGRectMake(0, rect.size.height - 1, rect.size.width, 1));
12     CGContextStrokeRect(context, separatorView.frame);
13 }

 效果:table

 

2.修改tableFooterView

1  self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

效果:class

相關文章
相關標籤/搜索