0

Warum habe ich einen Speicherverlust mit ARC aktiviert (fett hervorgehoben)?Benutzerdefinierte Zelle Speicherleck mit ARC aktiviert

Ich habe CustomCell.m

+(CustomCell*)cell 
{ 


    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPhone" owner:self options:nil];   
     return [nib objectAtIndex:0]; 

    } else { 
     NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell_iPad" owner:self options:nil];   **//leaking 100%** 
     return [nib objectAtIndex:0]; 

    } 
} 

In meinem Tableview conteroller:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell=[CustomCell cell]; **// 100% leaking** 
... 
} 

Antwort

1

Also, zwei Dinge. Eins, ich nehme an, Sie erstellen diese Zelle in einer .xib-Datei. Legen Sie die Wiederverwendungs-ID für die Zelle in IB fest. Dann wird anstelle dieser CustomCell Klassenmethode, entladen die Spitze in Tableview: cellForRowAtIndexPath :, wie folgt aus:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Assuming you set a reuse identifier "cellId" in the nib for your table view cell... 
    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"cellId"]; 
    if (!cell) { 
     // If you didn't get a valid cell reference back, unload a cell from the nib 
     NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]; 
     for (id obj in nibArray) { 
      if ([obj isMemberOfClass:[MyCell class]]) { 
       // Assign cell to obj, and add a target action for the checkmark 
       cell = (MyCell *)obj; 
       break; 
      } 
     } 
    } 

    return cell; 
} 

Die zweite Sache ist, dass eine wiederverwendbare Zelle, indem Sie versuchen zuerst aus der Warteschlange entfernt, werden Sie viel viel bessere Leistung zu bekommen.