2010-04-23 44 views
1

我希望刪除的UITablewView刪除行的UITableView導致內存泄漏

所有行的UITableView的是「atableview」,其數據來源是「fileArray」。

NSMutableArray * fileArray;

fileArray是對的NSMutableArray對象MYFileObj

#import <UIKit/UIKit.h> 

NSMutableArray *fileArray; 

@interface MYFileObj : NSObject { 

NSString *fileName; 

} 
-(void) setFileName:(NSString *)s ; 
-(NSString *) FileName ; 
@end 

我在第一加載fileArray,然後調用[atableview reloadData];

做一些事情之後,我希望重新加載fileArray和重繪atableview,所以我打電話

-(void) removeACell:(NSInteger)row; 
{         
    NSUInteger _lastSection = 0;//[self numberOfSectionsInTableView:atableview]; 
    NSUInteger _lastRow =row;// [atableview numberOfRowsInSection:_lastSection] - 1; 
    NSUInteger _path[2] = {_lastSection, _lastRow}; 
    NSIndexPath *_indexPath = [[NSIndexPath alloc] initWithIndexes:_path length:2]; 
    NSArray *_indexPaths = [[NSArray alloc] initWithObjects:_indexPath, nil]; 
    [_indexPath release]; 



    [atableview deleteRowsAtIndexPaths:_indexPaths withRowAnimation: UITableViewRowAnimationNone]; 

    [_indexPaths release]; 


} 


-(void) reloadList; 
{ 


if([fileArray count]>0) //----the begining of the codes cause memory leak 
    {     //I hope to remove all rows and reload fileArray 
     NSInteger n=fileArray.count; 
     [atableview beginUpdates]; 

     for(int i=n-1;i>=0;i--) 
     {   
      [fileArray removeObjectAtIndex:i]; 
      [self removeACell:i]; 


     } 
     [fileArray release]; 
     [atableview endUpdates]; 
    }     //----the end of the codes cause memory leak 

//load fileArray again 
[atableview reloadData]; 

} 

但是我發現這導致內存泄漏。

歡迎發表評論。

感謝

InterDev中

回答

1

雖然有要手動刪除一行或多行的時間,你的代碼似乎並沒有成爲這些情形之一的,因爲你再扭轉,並呼籲reloadData在tableview上。

當表中的內容發生變化時,首先對後備數據源進行更改 - fileArray - 然後調用reloadData,所有內容都會正常工作。接下來,如果您要完全清空它,則不必在循環中從數組中移除對象:只需使用[fileArray removeAllObjects]; (實際上,因爲你在循環之後釋放了fileArray,所以你可以將所有的邏輯減少到[fileArray release];並且它會向它的每一個對象發送一個版本

不知道你的mem漏洞在哪裏 - 有很多我們看不到的代碼,但按照所述的邏輯清理會幫助你。