2009-04-04 5 views
2

Lorsque je fais défiler la table trop bas, l'application est écrasée. Et je ne peux pas trouver où l'erreur dans le code est.L'application UITableView est écrasée lorsque le niveau de défilement est trop bas

Voici mon code:

NSMutableDictionary *tableDataDictionary; 
NSInteger *rows_in_section; 


-(void) initView 
{ 
    if (!tableDataDictionary) 
    { 
     tableDataDictionary = [ [NSMutableDictionary alloc] init]; 
    } 
    [self reloadTableData]; 
} 

-(void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:animated]; 
    [mainTableView deselectRowAtIndexPath: [mainTableView indexPathForSelectedRow] animated:NO]; 

    [tableDataDictionary release]; 
    tableDataDictionary = nil; 
    free(rows_in_section); 
} 

-(void) reloadTableData { 
    [tableDataDictionary removeAllObjects]; 


    //Sections 
    if (rows_in_section) 
    { 
     free(rows_in_section); 
    } 

    rows_in_section = (NSInteger*)calloc(25, sizeof(NSInteger)); 
    for (int i = 0; i <= 25; i++) 
    { 
     rows_in_section[i] = 0; 
    } 

    NSMutableArray *words_in_section = [ [NSMutableArray alloc] init]; 

    int cur_section = -1; 

    while (TRUE) 
    { 
     WordObject *tempWordObj = [ [WordsDatabase sharedWordsDatabase] getNextWordABC]; 
     if (!tempWordObj) 
     { 
      [tableDataDictionary setObject:words_in_section forKey:[NSNumber numberWithInt: cur_section] ]; 
      [tempWordObj release]; 

      [tableDataDictionary setObject:[words_in_section copy] forKey:[NSNumber numberWithInt: cur_section] ]; 
      [words_in_section removeAllObjects]; 

      break; 
     } 

     //First loop 
     if (cur_section == -1) cur_section = toupper([ [tempWordObj word] characterAtIndex:0 ]) - 'A'; 

     //If a has letter changed, assign the previous array to a dictionary 
     if ((toupper([ [tempWordObj word] characterAtIndex:0 ]) - 'A') != cur_section) 
     { 
      [tableDataDictionary setObject:[words_in_section copy] forKey:[NSNumber numberWithInt: cur_section] ]; 
      [words_in_section removeAllObjects]; 
     } 

     cur_section = toupper([ [tempWordObj word] characterAtIndex:0 ]) - 'A'; 

     [words_in_section addObject:tempWordObj]; 

     [tempWordObj release]; 
     tempWordObj = nil; 

     rows_in_section[cur_section]++; 

    } 

    [words_in_section release]; 

    [ [WordsDatabase sharedWordsDatabase] endGettingWordsABC]; 


    [mainTableView reloadData]; 
    [mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 

} 


// 
// Table handling 
// 

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

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return rows_in_section[section]; 
} 

// Adding a section index here 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return ALPHA_ARRAY; 
} 

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { 
    return index; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    DebugLog(@"cellForRowAtIndexPath"); 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 


    WordObject *tempWordObj = [ [tableDataDictionary objectForKey: [NSNumber numberWithInt:[indexPath section] ] ] objectAtIndex: [indexPath row] ]; 

    if (!tempWordObj) 
    { 
     DebugLog(@"!tempWordObj"); 
     return cell; 
    } 

    cell.text = [tempWordObj word]; 

    [tempWordObj release]; 

    return cell; 
} 

Comment puis-je résoudre ce problème avec le défilement vers le bas et pourquoi il se produit? Et si vous trouvez d'autres inconvénients dans mon code, s'il vous plaît dites-le moi. Nous vous remercions d'avance, Ilya.

Répondre

4

Vous n'avez pas besoin de cette ligne;

[tempWordObj release]; 

[NSDictionary objectForKey] retourne un objet libéré automatique, qui est l'objet sera automatiquement quelque temps après votre retour de la fonction.

+0

Merci, cela m'a vraiment aidé. –

Questions connexes