views:

562

answers:

2

Hi everyone, This is the first time I ask a question here, but I have to say this site has been a tremendous help for me over the last couple months (iphone-dev-wise), and I thank you for that.

However, I didn't find any solution for this problem I'm having: I have a UITableView with 2 sections, and no rows when the app is launched for the first time. The user can fill the sections later on as he wishes (the content is not relevant here). The UITableView looks good when filled with rows, but looks pretty ugly when there is none (the 2 header sections are stuck together with no white space in between). That is why I'd like to add a nice "No row" view in between when there is no row.

I used viewForFooterInSection:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

if(section == 0)
{
 if([firstSectionArray count] == 0)
  return 44;
 else 
  return 0;
}

return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if(section == 0)
    {
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)];
     label.backgroundColor = [UIColor clearColor];
     label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
     label.textAlignment = UITextAlignmentCenter;
     label.lineBreakMode = UILineBreakModeWordWrap; 
     label.numberOfLines = 0;
     label.text = @"No row";
     return [label autorelease];
    }

    return nil;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
    {
     return [firstSectionArray count];
    }
    return [secondSectionArray count];
}

This works great : the footer view appears only when there is no row in section 0. But my app crashes when I enter edit mode and delete the last row in section 0:

Assertion failure in -[UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cell animation stop fraction must be greater than start fraction'

This does not happen when there are several rows in section 0. It only happens when there is only one row left.

Here's the code for edit mode:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
     // Find the book at the deleted row, and remove from application delegate's array.

     if(indexPath.section == 0)
     { [firstSectionArray removeObjectAtIndex:indexPath.row]; }
     else 
     { [secondSectionArray removeObjectAtIndex:indexPath.row]; }

     // Animate the deletion from the table.
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
           withRowAnimation:UITableViewRowAnimationBottom];


     [tableView reloadData];

    }
}

Does anyone have any idea why this is happening? Thanks

A: 

it looks like what it happening is that internally Apple's table view code is assuming that when you delete a row, the first section of your view will get shorter. By making the section header footer suddenly get taller to compensate for the last row, you appear to be confusing it.

Two ideas for you to try:

  1. (very hacky) try making the optional section footer a pixel or two smaller than the table cell that's going away, so that the animation code gets to do a pixel or two of animation

  2. (slightly less hacky) instead of deleting the only row of your table, when there are no "real" data rows let the table still have numberOfRowsInSection return 1 and make a fake "no data" cell rather than using a table footer for the "no data" case.

This is one of those cases where you just have to accept that Apple has written half of your program for you and you have to conform to some choices your co-author has made, whether you like them or not.

David Maymudes
You are my hero! Making the "no data" cell 2 pixels smaller made it all work! Thank you so much! (and FYI, i had already thought of the second solution but could not make it work)I still have a slight problem though: the tableview's background is white now when there is no row in section 0. i liked it better when it was filled with empty cells (see image here: http://yfrog.com/izuitableviewproblemp). Any tip?Thank you again david, you made my day!
Supernico
no great ideas there, perhaps worth a separate question.
David Maymudes
ok, that's what I did. Thanks
Supernico
A: 

Hi, I was doing nearly the same, a button on the section header and clicking the button will collapse or expand the section rows. When collapse, show only the section header. But for collapsing, when I return 0 in numberOfRowsInSection: method, the section also disappears and header bg becomes black. any idea? thanks.

ashraf

karim
you should ask that in a new question, and also post some of your code for us to understand what's going on
Supernico