【问题标题】:remove rows of UITableView causes memory leak删除 UITableView 行导致内存泄漏
【发布时间】:2010-04-23 12:57:34
【问题描述】:

我希望删除 UITablewView 的所有行

UITableView是“atableview”,它的数据源是“fileArray”。

NSMutableArray *fileArray;

fileArray 是对象 MYFileObj 的 NSMutableArray

#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];

}

但我发现这会导致内存泄漏。

欢迎任何评论。

谢谢

交互开发

【问题讨论】:

    标签: iphone


    【解决方案1】:

    虽然有时您想手动删除一行或多行,但您的代码似乎不是其中一种情况,因为您随后转身并在 tableview 上调用 reloadData。

    当您的表发生变化时,首先对支持数据源进行更改 -- fileArray -- 然后调用 reloadData 一切都会好起来的。

    接下来,如果要完全清空数组,则不必在循环中从数组中删除对象:只需使用 [fileArray removeAllObjects]; (实际上,由于您在循环之后释放 fileArray,因此您可以将所有逻辑简化为 [fileArray release];它会向每个对象发送一个释放。

    不确定你的内存泄漏在哪里——有很多我们看不到的代码,但按照描述清理逻辑会帮助你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 2014-06-20
      • 2013-12-24
      • 1970-01-01
      • 2015-07-06
      • 2014-06-07
      • 2013-11-20
      • 2011-10-28
      相关资源
      最近更新 更多