Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate extra separators below UITableView

When I set up a table view with 4 rows, there are still extra separators lines (or extra blank cells) below the filled rows.

How would I remove these cells?

image for extra separator lines in UITableView

like image 818
RoundOutTooSoon Avatar asked Sep 02 '09 20:09

RoundOutTooSoon


People also ask

How to remove extra separator lines in a UITableView?

Add a Plain UIView to the Footer of the UITableView First, grab a plain UIView from the object browser, and drag it to the the footer position below all of your cell prototypes. It worked! The separator lines are gone.

How do I remove table view separator?

To hide UITableViewCell separator completely, simply set it's colour to UIColor. clearColor(). This will make the cell separator not visible.


2 Answers

Interface builder (iOS 9+)

Just drag a UIView to the table. In storyboard, it will sit at the top below your custom cells. You may prefer to name it "footer".

Here it is shown in green for clarity, you'd probably want clear color.

Note that by adjusting the height, you can affect how the "bottom bounce" of the table is handled, as you prefer. (Height zero is usually fine).

enter image description here


To do it programmatically:

Swift

override func viewDidLoad() {     super.viewDidLoad()     self.tableView.tableFooterView = UIView() } 

Objective-C

iOS 6.1+

- (void)viewDidLoad  {     [super viewDidLoad];      // This will remove extra separators from tableview     self.tableView.tableFooterView = [UIView new]; } 

or if you prefer,

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

Historically in iOS:

Add to the table view controller...

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {      // This will create a "invisible" footer      return CGFLOAT_MIN;  } 

and if necessary...

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {             return [UIView new];      // If you are not using ARC:     // return [[UIView new] autorelease]; } 
like image 139
20 revs, 6 users 77% Avatar answered Oct 04 '22 22:10

20 revs, 6 users 77%


Here's another way to do that w/out the grouped table style, and one you'd probably not guess. Adding a header and footer to the table (perhaps one or the other suffices, haven't checked) causes the separators to disappear from the filler/blank rows.

I stumbled onto this because I wanted a little space at the top and bottom of tables to decrease the risk of hitting buttons instead of a table cell with meaty fingers. Here's a method to stick a blank view in as header and footer. Use whatever height you like, you still eliminate the extra separator lines.

- (void) addHeaderAndFooter {     UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];     v.backgroundColor = [UIColor clearColor];     [self.myTableView setTableHeaderView:v];     [self.myTableView setTableFooterView:v];     [v release]; } 

In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.

Perhaps your custom views were not actually added. To check that, set the background color to something other than clearColor, e.g., [UIColor redColor]. If you don't see some red bars at the bottom of the table, your footer wasn't set.

like image 27
wkw Avatar answered Oct 04 '22 22:10

wkw