Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView-删除行会导致崩溃_Ios_Uitableview_Uitextfield - Fatal编程技术网

Ios UITableView-删除行会导致崩溃

Ios UITableView-删除行会导致崩溃,ios,uitableview,uitextfield,Ios,Uitableview,Uitextfield,我有一个带有自定义标题的UITableView,其中包含一个UITextField。我在textfield中输入文本,然后点击键盘上的“done”(完成)以关闭键盘-工作正常 我有一个UIActionSheet,它被启动以确认要删除表中的一行。点击“确认”按钮,操作表被取消,行被删除-正常工作 但是,如果我在textfield(并关闭键盘)中输入文本,然后尝试删除一行,则应用程序会在UIActionSheet上冻结片刻,然后崩溃 这是我收到的坠机报告 Thread 0 Crashed: 0

我有一个带有自定义标题的
UITableView
,其中包含一个
UITextField
。我在
textfield
中输入文本,然后点击
键盘上的“done”(完成)以关闭键盘-工作正常

我有一个
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的设备上运行它,并执行以下步骤:

  • 编辑文本字段
  • 关闭键盘
  • 滑动要删除的行
  • 点击删除按钮
  • 撞车
    感谢您查看此问题。

    您需要发布代码以获得更好的答案。如果您必须确保在tableview中添加和删除数据时,数据的大小是一致的,那么在没有看到它的情况下,我只能说。