views:

3419

answers:

4

When I set up the tableview say with 4 rows, there are still extra separators lines below the tableview (or extra blank cells)

How would I remove these cells (like in this case I just need 4 top cells)

    tblView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
[tblView setDelegate:self];
[tblView setDataSource:self];
[tblView setSeparatorStyle: UITableViewCellSeparatorStyleSingleLine];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

int row = [indexPath row];

return cell;}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}
+2  A: 

You could pass UITableViewStyleGrouped as the style of the UITableView when you call initWithFrame. Each section of the table will then display a distinct group of cells.

The first line of your example code would change to:

tblView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] 
                                             style:UITableViewStyleGrouped];
Daniel Richardson
thanks man, it works!
SimpleCode
+14  A: 

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.

wkw
Works great for me! Thanks for the tip, I didn't think of that solution, even though it makes perfect sense that this should work.
Mirko Froehlich
+1: Works great now. Setting the header view seems to be unnecessary and in my opinion makes it look worse
Casebash
Right. I doubt the header view would matter for the lines below. I merely copy/pasted the method I used where I DID want some extra space above the table. I didn't think to point out that only the footer was relevant.
wkw
A: 

I was using a table view to show a fixed number of columns, so I simply resized it and made it non-scrollable.

Casebash
+1  A: 

Another way to do it after the table has been inited is to simply assign a clear color to the separator as follows:

[tblView setSeparatorColor:[UIColor clearColor]];