tableView去掉多餘的空行分割線

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

我們經常會遇到下面的問題,tableView表視圖上面的內容不是很多,但是 tableView 卻幫忙把 整個屏幕都用 空白行分割線佔滿了:

如下圖:


代碼如下:

  1. //  
  2. //  TableViewController.m  
  3. //  Test  
  4. //  
  5. //  Created by  on 15/1/25.  
  6. //  Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga. All rights reserved.  
  7. //  
  8.   
  9. #import "TableViewController.h"  
  10.   
  11. @interface TableViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation TableViewController  
  16.   
  17. - (void)viewDidLoad {  
  18.     [super viewDidLoad];  
  19.       
  20. }  
  21.   
  22. #pragma mark - Table view data source  
  23.   
  24. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  25.     return 1;  
  26. }  
  27.   
  28. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  29.     return 3;  
  30. }  
  31.   
  32. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  33. {  
  34.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];  
  35.     return cell;  
  36. }  
  37.   
  38. @end  
可以看到本來只有3行數據,卻顯示了很多行的空白分割線,如何去掉?

方法1. 完全去掉所有的分割線,然後 在cell上自定義 一個 view高度爲一個像素,來模擬真實的 分割線

2.方法二,如果不想自定義分割線的話,那就來一個粗暴的方法吧,增加一個  footerView即可解決問題代碼如下:

  1. //  
  2. //  TableViewController.m  
  3. //  Test  
  4. //  
  5. //  Created by  on 15/1/25.  
  6. //  Copyright (c) 2015年 http://blog.csdn.net/yangbingbinga. All rights reserved.  
  7. //  
  8.   
  9. #import "TableViewController.h"  
  10.   
  11. @interface TableViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation TableViewController  
  16.   
  17. - (void)viewDidLoad {  
  18.     [super viewDidLoad];  
  19.     <span style="font-size:24px;"><strong>self.tableView.tableFooterView=[[UIView alloc]init];//關鍵語句  
  20. </strong></span>      
  21. }  
  22.   
  23. #pragma mark - Table view data source  
  24.   
  25. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {  
  26.     return 1;  
  27. }  
  28.   
  29. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {  
  30.     return 3;  
  31. }  
  32.   
  33. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  34. {  
  35.     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];  
  36.     return cell;  
  37. }  
  38.   
  39. @end  
下面,看一下運行效果,看看是不是輕鬆解決了呢?

本文出處:http://blog.csdn.net/yangbingbinga

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章