Note that there are some explanatory texts on larger screens.

plurals
  1. POButton added to custom section header view disappears when row is deleted
    primarykey
    data
    text
    <p>Just came across some very strange behavior in my app. I've recreated the problem in a simplest-case scenario:</p> <pre><code>NSMutableArray *data; - (void)viewDidLoad { [super viewDidLoad]; data = [[NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil] retain]; } - (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section { UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)]; header.backgroundColor = [UIColor lightGrayColor]; [header addSubview:self.button]; return header; } - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [data removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return data.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease]; cell.textLabel.text = [data objectAtIndex:indexPath.row]; return cell; } - (void)dealloc { [super dealloc]; } </code></pre> <p>Every time I delete a row; the button in my header disappears! This happens no matter what type of rowAnimation I use. If I scroll the table up so that the header scrolls off; the button returns when the header returns. The button is created in the xib file.</p> <p>I can work around it in one of 2 ways:</p> <ul> <li>Reloading the tableView data after the delete; with a delay so that the deletion animation completes first.</li> <li><p>Creating the button in viewForHeaderInSection instead of in the interfaceBuilder. </p> <p>I'd really like to understand what's going on here. Where is the button going? I've confirmed that viewForHeaderInSection is called when I delete a row.</p></li> </ul> <p><em>Edit</em> I tried changing it so that the button is created in viewForHeader, instead of in the xib, but it's causing other strange issues... when I create or delete the button, I am setting certain properties such as the title and enabled depending on how many items there are in the table. When I delete the last row in the table, I don't see the update in text and enabled status until I scroll the button off the screen and back on again.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload