UITableView - 删除行会导致崩溃

时间:2012-10-28 16:58:54

标签: ios uitableview uitextfield

我有UITableView个自定义标头,其中包含UITextField。我在textfield中输入文字并点按keyboard上的“完成”以解除键盘 - 工作正常。

我启动了UIActionSheet以确认是否要删除表中的行。点击“确认”,UIActionSheet被取消,行被删除 - 工作正常。

但是,如果我在textfield中输入文字(并关闭键盘),然后尝试删除一行 - 该应用会在UIActionSheet上冻结片刻,然后崩溃。

这是我得到的崩溃报告。

Thread 0 Crashed:

0   CoreFoundation        0x00004260 __CFTypeCollectionRetain + 16
1   CoreFoundation        0x00005e38 __CFDictionaryRetainValue + 20
2   CoreFoundation        0x000054a8 __CFBasicHashAddValue + 100
3   CoreFoundation        0x00005158 CFDictionarySetValue + 68
4   UIKit                 0x000e3472 -[UITableView(_UITableViewPrivate) _updateWithItems:withOldRowData:oldRowRange:newRowRange:context:] + 1730
5   UIKit                 0x000e281e -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 4770
6   UIKit                 0x0012a4d6 -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] + 198
7   UIKit                 0x0012a3de -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] + 14
8   Tracker8              0x00012502 -[TrackerTableViewController actionSheet:clickedButtonAtIndex:] (PriceTableViewController.m:313)
9   UIKit                 0x00369596 -[UIActionSheet(Private) _buttonClicked:] + 186

为什么这两个元素单独工作但不按顺序工作的任何建议?谢谢你的帮助。

更新:我的代码的更多测试显示崩溃仅发生在运行iOS 4.1的设备上。它不会在运行iOS 5.1或iOS 6.0的设备上崩溃。这让我觉得我正在处理一个旧的,可能已知的错误。

我通过将tableview定位在IB中使用标签和文本字段创建的模拟“标题视图”下方来解决这个问题。如果编辑了文本字段,然后删除了一行,则不会再崩溃。

对于那些有兴趣重新创建/解释崩溃的人来说,这里是实现代码:

@synthesize table;
@synthesize array;

#pragma mark View Lifecycle

- (void)viewDidLoad 
{
    [super viewDidLoad];

    NSMutableArray *staticArray = [[NSMutableArray alloc] initWithObjects:@"This is the 1st row.",
                        @"This is the 2nd row.", @"This is the 3rd row.",
                        @"This is the 4th row.", @"This is the 5th row.",
                        @"This is the 6th row.", @"This is the 7th row.",
                        @"This is the 8th row.", @"This is the 9th row.",
                        @"This is the 10th row.", nil];

    self.array = staticArray;
    [staticArray release];
}

#pragma mark TableView Data Source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    return [self.array count];
}

- (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];
        [cell autorelease];
    }

    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
                                        forRowAtIndexPath:(NSIndexPath *)indexPath 
{

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    {
        // remove selected object from the data array
        [self.array removeObjectAtIndex:indexPath.row];

        // Delete the row from the table.
        [self.table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:UITableViewRowAnimationFade];                
    } 
}

#pragma mark TableView Delegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // custom header view containing a label and a textfield
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]autorelease];
    headerView.backgroundColor = [UIColor darkGrayColor];
    headerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    UILabel *pLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 21)] autorelease];
    pLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    pLabel.backgroundColor = [UIColor clearColor];
    pLabel.textAlignment = UITextAlignmentCenter;
    pLabel.textColor = [UIColor blackColor];
    pLabel.font = [UIFont boldSystemFontOfSize:17.0];
    pLabel.adjustsFontSizeToFitWidth = YES;
    pLabel.minimumFontSize = 10.0;
    pLabel.text = @"Table Header"; 

    UITextField *pField = [[[UITextField alloc] initWithFrame:CGRectMake(20, 18, 280, 31)] autorelease];
    pField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    pField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    pField.textAlignment = UITextAlignmentCenter;
    pField.textColor = [UIColor blackColor];
    pField.font = [UIFont systemFontOfSize:17];
    pField.adjustsFontSizeToFitWidth = YES;
    pField.minimumFontSize = 10.0;
    pField.borderStyle = UITextBorderStyleNone;
    pField.clearButtonMode = UITextFieldViewModeWhileEditing;
    pField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    pField.autocorrectionType = UITextAutocorrectionTypeNo;
    pField.keyboardType = UIKeyboardTypeDefault;
    pField.returnKeyType = UIReturnKeyDone;
    pField.delegate = self; 
    pField.placeholder = @"tap to enter text";

    // add label and textField to the header view
    [headerView addSubview:pLabel];
    [headerView addSubview:pField];

    return headerView;  
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0;
}

#pragma mark TextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{   
    // dismiss the keyboard
    [textField resignFirstResponder];

    return YES;
}

#pragma mark Memory Management

- (void)didReceiveMemoryWarning 
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload 
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;

    self.table = nil;
}

- (void)dealloc 
{
    [table release];
    [array release];

    [super dealloc];
}

@end

要使其崩溃,请在使用iOS 4.1的设备上运行它,然后按照以下步骤操作:

  1. 编辑文本字段
  2. 关闭键盘
  3. 滑动一行以删除
  4. 点按删除按钮
  5. CRASH!
  6. 感谢您查看此问题。

0 个答案:

没有答案