Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView section header disappears if I insert or delete rows

I have a UITableView with 4 sections. Three of these sections have a header view.

The header view is a UITableViewCell, which I am de-queueing as a normal cell in the viewForHeaderInSection: delegate.

If I insert or delete a row from any section, the other tableview header cells disappear.

I'm assuming this has something to do with cell reuse, however the cells initially all appear on screen (all three headers appear onscreen at the same time).

I've tried reloading the other sections after the insert or delete, but that doesn't help.

Here's some code:

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {      switch (section) {          case kSectionCardInformation:         case kSectionContactInformation:         case kSectionTags: {              static NSString *CellIdentifier = @"EditContactHeaderCell";             EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];              return cell;         }          default:             return nil;     } } 

And here is where I delete the row in the revelant section:

- (void)deleteTag:(CDTag *)tag {      [self.tableView beginUpdates];      NSMutableArray *objects = [self.sections objectAtIndex:kSectionTags];     if ([objects containsObject:tag]) {         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[objects indexOfObject:tag] inSection:kSectionTags];         [objects removeObject:tag];          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];          [self.contact deleteTag:tag];     }      [self.tableView endUpdates]; } 

Any help, greatly appreciated.

like image 446
theDuncs Avatar asked May 02 '14 09:05

theDuncs


People also ask

How do I scroll the header along with UITableView?

If you have a single header in the table then you can use tableHeaderView as below: tableView. tableHeaderView = Header; Or if you have multiple header in table than you need to use Group table instead of plain table.

How do I hide a tableView section?

You can't "hide" a section as such, but you can "delete" it from the table view using the deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)


1 Answers

Just wrap your UITableViewCell into a UIView.

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {  switch (section) {      case kSectionCardInformation:     case kSectionContactInformation:     case kSectionTags: {          static NSString *CellIdentifier = @"EditContactHeaderCell";         EditContactHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];         UIView * view = [[[UIView alloc] init] autorelease];         [view addSubview:cell];         //setup the view's frame: fixed, autolayout, ...         return view;     }      default:         return nil; } 

}

like image 59
louissmr Avatar answered Oct 10 '22 14:10

louissmr