2010-04-23 46 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漏洞在哪里 - 有很多我们看不到的代码,但按照所述的逻辑清理会帮助你。