2012-01-30 179 views
0

在.h文件中,我設置了下面的行。removeObjectAtIndex崩潰應用程序

NSMutableArray *repArray; 

在我已放置在下面線.m文件(我把這種前的代碼只是所需的線) - (無效)read_data_fromDB { objectsForCharacters = [[ALLOC的NSMutableDictionary] 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]; 

兩個崩潰,即使我有在陣列多於一個元素的應用程序。

+0

什麼是錯誤信息? – Nyx0uf 2012-01-30 14:36:14

+0

@Philippe Sabourin我不知道如何接受我以前的問題的答案。 – dinesh 2012-01-30 15:45:01

+0

@Benj我沒有得到任何錯誤消息。它在模擬器中正常工作,沒有錯誤。但不是在iPod – dinesh 2012-01-30 15:45:40

回答

1

當你的應用程序崩潰時,99%的時間你試圖刪除超出數組邊界的對象。

+0

可能是,但爲什麼我有這個問題不存在於模擬器中。 – dinesh 2012-01-30 15:46:47

+0

彼得你能幫我解決這個問題嗎? – dinesh 2012-01-31 08:51:31

+0

我可以嘗試 - 但你需要發佈更多的代碼 - 你在哪裏創建數組和更多的代碼在上下文中從數組中刪除對象。另外,您是否檢查過indexPath.row小於數組對象數? – 2012-01-31 09:33:09