tags:

views:

32

answers:

1

Hello everyone

I hope to delete all rows of an UITablewView

The UITableView is "atableview", its data source is "fileArray".

NSMutableArray *fileArray;

fileArray is NSMutableArray for object MYFileObj

#import <UIKit/UIKit.h>

NSMutableArray *fileArray;

@interface MYFileObj :  NSObject  {

NSString *fileName;

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

I load fileArray at first, then call [atableview reloadData];

after do something, I hope to reload fileArray and redraw atableview, so I call

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

}

But I found this cause memory leak.

Welcome any comment.

Thanks

interdev

+1  A: 

While there are times where you want to delete a row or rows manually, your code doesn't seem to be one of those cases because you're then turning around and calling reloadData on the tableview.

when things change in your table, make the changes to the backing data source first -- fileArray -- and then call reloadData and all will be fine.

Next, you don't have to remove objects from the array in a loop if you're emptying it completely: just use [fileArray removeAllObjects]; (actually, since you're releasing fileArray after your loop, you could reduce all that logic to [fileArray release]; and it will send a release to each of it's objects.

Not sure where your mem leak is -- there's plenty of code we can't see, but cleaning up the logic as described will help you out.

wkw