removeObjectAtIndex使应用程序崩溃

时间:2012-01-30 14:30:46

标签: objective-c nsmutablearray nsarray

在.h文件中,我设置了以下行。

  NSMutableArray *repArray;   

在.m文件中我放置了以下行。(我只为这个ex放了所需的代码行) - (无效)read_data_fromDB {     objectsForCharacters = [[NSMutableDictionary alloc] init];

sqlite3 *db = [contactAppDelegate getNewDBConnection];

NSMutableString *query = nil;

query = [NSMutableString stringWithFormat:@"select representative, title, business_name, uid from corporate where profiler_type <> 'OWN_CORPORATE' order by representative asc"];  

const char *sql = [query UTF8String];
sqlite3_stmt *selectAllStmt = nil;

if(sqlite3_prepare_v2(db,sql, -1, &selectAllStmt, NULL)!= SQLITE_OK)
    NSAssert1(0,@"error preparing statement",sqlite3_errmsg(db));
else
{
    repArray = [[NSMutableArray alloc]init];
    businessArray = [[NSMutableArray alloc]init];
    titleArray = [[NSMutableArray alloc]init];
uidArray = [[NSMutableArray alloc]init];

    while(sqlite3_step(selectAllStmt)==SQLITE_ROW)
    {


        char *chrstr =(char *)sqlite3_column_text(selectAllStmt, 0);
        if(chrstr !=NULL)
        {
            corpRepresentative = [NSString stringWithUTF8String:chrstr];
            [repArray addObject:corpRepresentative];
        }       
        chrstr =(char *)sqlite3_column_text(selectAllStmt, 1);
        if(chrstr !=NULL)
        {

            corpTitle = [NSString stringWithUTF8String:chrstr];
            [titleArray addObject:corpTitle];
        }
        chrstr =(char *)sqlite3_column_text(selectAllStmt, 2);
        if(chrstr !=NULL)
        {
            corpBusiness_name = [NSString stringWithUTF8String:chrstr];
            [businessArray addObject:corpBusiness_name];

        }   

        chrstr =(char *)sqlite3_column_text(selectAllStmt, 3);
        if(chrstr !=NULL)
        {
            corporteUid = [NSString stringWithUTF8String:chrstr];
            [uidArray addObject:corporteUid];

        }


    }


}
sqlite3_finalize(selectAllStmt);

sqlite3_close(db);

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 

{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    UILongPressGestureRecognizer *longPressGesture =
    [[[UILongPressGestureRecognizer alloc]
      initWithTarget:self action:@selector(longPress:)] autorelease];
    [cell addGestureRecognizer:longPressGesture];
}

// Configure the cell...

UILabel *txtLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 0, 175, 26)];
//[txtLbl setBackgroundColor:[UIColor greenColor]];
[txtLbl setTextAlignment:UITextAlignmentLeft];
NSString *txtStr = [repArray objectAtIndex:indexPath.row];
UIFont *fontTxtLbl =  [UIFont fontWithName: @"FuturaMdBT" size:16];
[txtLbl setFont:fontTxtLbl];
[txtLbl setText:txtStr];
[cell.contentView addSubview:txtLbl];
[txtLbl release];


    UILabel *txtDetailLbl = [[UILabel alloc]initWithFrame:CGRectMake(70, 27, 175, 20)];
   // [txtDetailLbl setBackgroundColor:[UIColor redColor]];
    [txtDetailLbl setTextAlignment:UITextAlignmentLeft];
    NSString *txtDetailStr = [titleArray objectAtIndex:indexPath.row];
    UIFont *fontTxtDetailLbl =  [UIFont fontWithName: @"Arial" size:12];
    [txtDetailLbl setFont:fontTxtDetailLbl];
    [txtDetailLbl setText:txtDetailStr];
    [cell.contentView addSubview:txtDetailLbl];
    [txtDetailLbl release];


UILabel *txtDetailLbl1 = [[UILabel alloc]initWithFrame:CGRectMake(70, 47, 175, 20)];
//[txtDetailLbl1 setBackgroundColor:[UIColor blueColor]];
[txtDetailLbl1 setTextAlignment:UITextAlignmentLeft];
NSString *txtDetailStr1 = [businessArray objectAtIndex:indexPath.row];
UIFont *fontTxtDetailLbl1 =  [UIFont fontWithName: @"Arial" size:12];
[txtDetailLbl1 setFont:fontTxtDetailLbl1];
[txtDetailLbl1 setText:txtDetailStr1];
[cell.contentView addSubview:txtDetailLbl1];
[txtDetailLbl1 release];

   // cell.textLabel.text = [repArray objectAtIndex:indexPath.row];
    //cell.detailTextLabel.text = [titleArray objectAtIndex:indexPath.row];
    cell.imageView.image=[imagearray objectAtIndex:indexPath.row];






UIView *viewSelected = [[[UIView alloc] init] autorelease];
viewSelected.backgroundColor = [UIColor colorWithRed:224.0/255.0
                                               green:229.0/255.0
                                                blue:241.0/255.0
                                               alpha:1.0];
cell.selectedBackgroundView = viewSelected;

cell.textLabel.highlightedTextColor=[UIColor colorWithRed:46.0/255.0 
                                                    green:77.0/255.0 
                                                     blue:141.0/255.0 
                                                    alpha:1.0];


return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{



    NSString *tempUID = [uidArray objectAtIndex:indexPath.row];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:tempUCID forKey:@"corpUcid"];


    [prefs setObject:@"PROFILER" forKey:@"selProfiler"];

    CorpPrevFront *corpPrevFront = [[CorpPrevFront alloc]initWithNibName:@"CorpPrevFront" bundle:nil];
    [self.navigationController pushViewController:corpPrevFront animated:NO];
    [corpPrevFront release];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];



}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //FOR VIEW
    if (buttonIndex == 1 && [alertView tag] == 1) {
        // IT WILL BE MATCH ON CorpPrevFront CLASS

    }
     //FOR DELETE
    if(buttonIndex == 2 && [alertView tag] == 1){



    } else if(buttonIndex == 1 && [alertView tag] == 2){
        DeleteProfiler *delProfiler = [[DeleteProfiler alloc]init];

        BOOL del = [delProfiler deleteProfiler:selectedCpUid];
         //[delProfiler deleteProfiler:selectedCpUcid];
        if (del == YES) {


          [repArray removeObjectAtIndex:selectedIndexPath.section];
            [businessArray removeObjectAtIndex:selectedIndexPath.row];
            [titleArray removeObjectAtIndex:selectedIndexPath.row];
            [ucidArray removeObjectAtIndex:selectedIndexPath.row];



        [self.corporateTable reloadData];



                   }


    }
}

我正在尝试从方法中删除数组元素。如果我执行以下行,那么应用程序会在我的iPod Touch中自动崩溃。但它在模拟器中工作正常,没有任何错误。

[repArray removeObjectAtIndex:selectedIndexPath.row];

[repArray removeObjectAtIndex:0];
即使我在数组中有多个元素,

也会崩溃应用程序。

1 个答案:

答案 0 :(得分:1)

当你的应用程序崩溃时,99%的时间你试图删除超出数组范围的对象。