views:

43

answers:

1

So I'm totally stumped by this one and tempted to call "OS bug".

I have a TableView controller with a single section, and in the header for that section there is an UITextField. Several operations result in rows being added/removed without a problem. However, as soon as text is edited in the header, and the keyboard dismissed, any insertion/removal of rows results in an immediate crash.

And it can actually be simplified further - simply calling beginUpdates/endUpdates on the table once the keyboard is dismissed is enough to cause a crash. The end of the callstack is:

_CFTypeCollectionRetain
_CFBasicHashAddValue
CFDictionarySetValue
-[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:]
-[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:]
-[UITableView endUpdates]

I've put together a minimal example that demonstrates the problem.

Complete Controller source: http://www.andrewgrant.org/public/TableViewFail.txt

Example Project: http://www.andrewgrant.org/public/TableViewCrash.zip

Most relevant code:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // create header view
    UIView* header = [[[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 320.f, 50.f)] autorelease];

    // text field
    UITextField* textField = [[[UITextField alloc] initWithFrame:CGRectMake(10.f, 12.f, 300.f, 28.f)] autorelease];
    textField.text = @"Edit, then 'Save' will crash";
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.clearButtonMode = UITextFieldViewModeAlways;
    textField.delegate = self;

    [header addSubview:textField];

    return header;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // no purpose, but demonstrates updates work at this point
    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [textField resignFirstResponder];

    // immediate crash
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    return YES;
}
A: 

Just an update - I submitted a bug report and repro case to Apple and they confirmed it's a bug in iOS 4.0. As of iOS 4.1 beta 2 it had not been fixed.

My work around was to turn the first row of my table into a pseudo header that occupies the entire content view and has a custom height. It's not quite as good (things cant reach to the edge of the screen for example) but it's close and doesn't crash.

Andrew Grant