ios - delete single files that are downloaded into the documents directory - ios

Firstly = I apologize because I have already tried to ask this once before here
I am really struggling with this:
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
By using the code above I know it is possible to delete an entry from an array that is displayed in a UITableView. However I am wanting to delete files form my Documents Directory that are downloaded by the user and no longer required.
Now I in the mean time and after more searching this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *file = [[NSString alloc] init];
for(int i=0; i<[paths count]; i++)
{
file = [documentsDirectoryPath stringByAppendingFormat:#"/%#",_fileArray];
NSLog(#"%#", file);
}
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:file error:NULL];
Allows me to list all the files that are in the array in my console and this code:
- (void)removeFile:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.mp3", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
together with: [self removeFile: _filename]; allows me to delete a specific file.
so I am making head way. But I am really stuck when it comes to a user being able to swipe and delete a file. Of Course I do not know what file is going to be in the directory.
Secondly - How do I handle being able to load the tableView once all the files are deleted?
If I do that using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] > 0)
{
NSLog(#"Path: %#", [paths objectAtIndex:0]);
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
// Remove Documents directory and all the files
BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
}
I get the an termination error - I think that is because the directory has also been removed.
I know there is a bit of code here, but I really hope someone and guide me through this:-)

If i correctly understood, you want step by step guide.
Lets consider that you have a working UITableView (mainTableView) that take data from NSMutableArray (mainArray).
This will return document directory path.
-(NSString*)filePath{
NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSLog(#"%#",path);
return path;
}
Somewhere we need to init array ,i did it on viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
mainDataArray=[[NSMutableArray alloc]init];
[self loadContentsOfDirectory:[self filePath]];
}
//here i am adding names of files from documents directory
-(void)loadContentsOfDirectory:(NSString*)path
{
NSError *error=nil;
NSArray *pathArray=[[NSFileManager defaultManager]contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(#"ERROR: %#",error);
}else{
if (pathArray) {
[mainDataArray removeAllObjects];
[mainDataArray addObjectsFromArray:pathArray];
}
}
}
#pragma mark UITableView_Methods
//delete file then if file is deleted , remove filename from array and remove cell
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSString *fileName=[mainDataArray objectAtIndex:indexPath.row];
NSError *error=nil;
NSString *pathToDelete=[[self filePath]stringByAppendingPathComponent:fileName];
BOOL succes=[[NSFileManager defaultManager]removeItemAtPath:pathToDelete error:&error];
if (error) {
NSLog(#"ERROR: %#",error);
}
// if file is succes deleted
if (succes) {
//remove this item from array
[mainDataArray removeObject:fileName];
//and remove cell
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}else{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:#"Alert!" message:#"File can not be deleted" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alertView show];
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [mainDataArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reusableCell=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reusableCell];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusableCell];
}
cell.textLabel.text=[mainDataArray objectAtIndex:indexPath.row];
return cell;
}
I think your error is that you do not remove something maybe cell, object in array.
Hope this will help.

If you want to delete selected files from your Documents directory then.
First you have to get all files from Documents directory of your app like this
-(NSArray *)listFileAtPath:(NSString *)path
{
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
return directoryContent;
}
Then use return array from above function to display files to UITableView
NSArray *filesArray = [self listFileAtPath:documentDirectoryPath];
And finally when you delete selected files then you have to remove that file from Documents directory and also from filesArray for managing UITableView something like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete the row from the data source.
[_mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Remove file from document directory
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL deleted = [fileManager removeItemAtPath:[paths objectAtIndex:0] error:&error];
// Remove file entry from filesArray
[filesArray removeObjectAtIndex:indexPath.row];
}
}
Hope will help you if you want to do something like this.

Related

tableView reloadData not reloading data after deletion in documents directory

I have a problem where i cant seem to reload the data in my tableView.
What i am trying to do is populate my tableView with files in the documents directory with a csv extension (which i have done), then press a button which deletes all the displayed files and the updates the tableView.
The button when pressed deletes the files in the documents directory but does not reload the tableView with the updated content.
I have used breakpoints to confirm that reloadData is being run.
I have tried using removeAllObjects to clear my array before running
reloadData using NSLog to confirm the array is empty, however this
does not update the table.
I have tried setting the array to nil before running reloadData, this does not work either.
I have tried setting the data in the array to myArray = #[]; before
running reloadData, this overwrites the array with blank data but
does not update the table.
I have tried using this method without
any success:
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
I have checked my delegates for the tableView and they appear
correct, (all other functionality in the tableView works including
selecting/deselecting multiple items and deleting individual items
and on each occasion the tableView updates).
I have also searched multiple different questions similar to mine and still have not found an answer.
Could someone please check my method below and tell me why the tableView is not reloading properly? i'm sure its something i have done wrong somwhere but i cant find it.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
_docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [_docPath objectAtIndex:0];
_fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.csv'"];
_csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
NSLog(#"Contents of directory: %#", _csvFilesCell);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [_csvFilesCell objectAtIndex:indexPath.row];
return cell;
}
-(IBAction) deleteStoredData:(id)sender
{
NSLog(#"Delete data button pressed");
UIAlertController*alert = [UIAlertController alertControllerWithTitle:#"Delete stored Data" message:#"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* OK = [UIAlertAction actionWithTitle:#"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSFileManager *manager = [NSFileManager defaultManager];
// the preferred way to get the apps documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// grab all the files in the documents dir
NSArray *allFiles = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
// filter the array for only csv files
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.csv'"];
NSArray *csvFiles = [allFiles filteredArrayUsingPredicate:fltr];
// use fast enumeration to iterate the array
for (NSString *csvFile in csvFiles) {
NSLog(#"PATH %# : FILE %#", documentsDirectory, csvFile);
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:#"%#/%#", documentsDirectory, csvFile]
error:&error];
NSLog(#"Error: %#", error);
NSLog(#"OK button selected, all data deleted");
[self updateDeleteButton];
[self.tableView reloadData];
[alert dismissViewControllerAnimated:YES completion:nil];
}
}];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(#"Cancel button selected, no data deleted");
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:OK];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
After seeing your code, I came up with the following solution, hope it helps you !!
_docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [_docPath objectAtIndex:0];
_fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:#"self ENDSWITH '.csv'"];
_csvFilesCell = [_fileList filteredArrayUsingPredicate:fltr];
NSLog(#"Contents of directory: %#", _csvFilesCell);
filePathsArray = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:documentsDirectory error:nil];
The above code should be triggered at the click of delete button, not in the tableview's data-source method. After that you should reload your tableview and your cellForRowAtIndexPath method should be like :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [_csvFilesCell objectAtIndex:indexPath.row];
return cell;
}

Populating TableView with Plist Documents Directory in iOS

I'm a newbie to Objective-C so any help is greatly appreciated.
I have a TableView that is successfully pulling back the list from my Data.plist file within my Xcode project but I need it to pull from the Documents Directory.
I have seen many posts about this but can't seem to get it to work for me.
Here is my .m file below. Like I said, it pulls back the data but I need it to be dynamically changed based on my Documents plist copy.
Thanks in advance!
#synthesize content = _content;
- (NSArray *)content
{
if (!_content) {
_content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"]];
}
return _content;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.content count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Name"];
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:#"Score"];
return cell;
}
Try this:
Step 1: In .h File declare a global variable
#property (nonatomic,retain) NSMutableDictionary *contents;
Step 2: In .m file set synthesis.
#synthesize contents;
Step 3: Move plist file from mainbudle into documents directory
-(void) createPlistDocuments
{
// Get path to documents directory
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// Finds the contained Documents directory
NSString *documentsDirectory = [arrayPaths objectAtIndex:0];
NSError *error;
// Create an object that we will later use to look for a file and return a boolean value on whether or not it exists
NSFileManager *manager = [NSFileManager defaultManager];
// File we want to move, stored in original top level directory
NSString *demoFile = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
// Define where we want it moved to and name it
NSString *demoFileMoved = [NSString stringWithFormat:#"%#/data.plist", documentsDirectory];
// Attempt the copy
if ([manager copyItemAtPath:demoFile toPath:demoFileMoved error:&error] != YES)
NSLog(#"Unable to move file: %#", [error localizedDescription]);
}
Step 4: Read plist data from document directory
- (void)viewDidLoad
{
[super viewDidLoad];
[self createPlistDocuments];
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:#"%#/data.plist",documentsDirectoryPath];
contents = [NSArray arrayWithContentsOfFile:path];
NSLog(#"%d", contents.count);
}
For brief explanation follow this sample... TechDevMobile(IOS-Message-Chat)

iOS Removing Object From Array

I am loading an array of items from a plist. A user can select any item and add it to a new array of items which is shown on screen.
I am doing this but a user can also delete a player from any row. My problem is when a user deletes the object it shuffles the list up. Where as I want to keep that row blank.
My deletion code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *user_id = [prefs objectForKey:#"user_id"];
NSString *fixture_id = [prefs objectForKey:#"fixture_id"];
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
[self.usersSelectionArray removeObjectAtIndex:indexPath.row];
if (self.usersSelectionArray == nil) {
// disable swipe to delete?
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-%#",user_id, fixture_id]];
// Write array
[self.usersSelectionArray writeToFile:arrayPath atomically:YES];
}
// If blank array delete the file as not required
if (self.usersSelectionArray.count == 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#-%#", user_id, fixture_id]];
NSError *error;
if(![[NSFileManager defaultManager] removeItemAtPath:path error:&error])
{
//TODO: Handle/Log error
}
}
[self.tableView reloadData];
}
It also checks if the count is 0 and deletes the plist, re-creates on load if required.
So you want to remove an object from your data array, but keep an empty cell for it in the table?
In that case I recommend replacing the removed object with an NSNull instance.
[myMutableArray replaceObjectAtIndex:index withObject:[NSNull null]];
You can then check if the object for a cell is NSNull and return an empty cell in the cellForRowAtIndexPath: data source method.
id object = // get the correct object from your data array, depending on the index
BOOL isEmpty = [object isKindOfClass:[NSNull class]];
if (isEmpty) {
// return empty table cell
else {
// return a normal cell
}
Instead of [self.usersSelectionArray removeObjectAtIndex:indexPath.row];, try [self.usersSelectionArray replaceObjectAtIndex:indexPath.row withObject:[NSNull null]];. That will keep the row but it will contain practically nothing

Objective-C: how to list files in individual UITableViewCells? [duplicate]

This question already has answers here:
Objective-C: how to add a new cell for each filename in UiTableView?
(2 answers)
Closed 9 years ago.
Bellow I have some code that retrieves files from the documents directory where my application is stored on the idevice. It then successfully retrieves that path to the file which is a mp4 because that is what I am storing. And does this by displaying the filename in a cell of the UiTableView I have created . However the code only displays one file in one cell. But I want to list multiple files individually in there own cell so eventually the user can individually select that cell depending on the video file they want.
Here is the code that retrieves and displays the file directory of the file but does not list more then one file in the table:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray) // if data loading has been completed, return the number of rows ELSE return 1
{
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 1;
}
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray)
{
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 1;
}
return 1;
}
Should read:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [filePathsArray count];
}
You should be returning [filePathsArray count]; from your numberOfRowsInSection method under normal circumstances. Right now you're actually returning 1 from that method in all cases. I'm not sure what exactly you're trying to do with the logic at the top of your numberOfRowsInSection method, but I think you should start by just replacing the whole body of the method with return [filePathsArray count];.
One more thing: You have a comment in there "if data loading has completed," but all the work in the code you posted is going synchronously on the main thread. By the time your viewDidLoad method has completed your filePathsArray is initialized. You don't need to do anything tricky to account for a case where filePathsArray is still nil, unless you're worried about the table view loading data before the view controller's view has actually loaded. But that would be very weird.
Why are you initializing filePathsArray over and over again first under viewDidLoad and then under tableview.
This is how I would write the code.
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//initializing array and getting values the first time when page loads.
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//return whatever the count of this array is. no need to do any if! checks
return [filePathsArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
return cell;
}

Objective-C: How to refresh a UITableView?

I have some code that displays file name in my uitableview. however once deleting a file and refreshing i receive an error. Here is my code to display my file name, my delete button actions and the error:
Firstly when a button is pressed i run this code which works when adding a file to the table:
-(IBAction)refresh{
[[self mytable] reloadData];
}
Secondly I have this code to get and display the values the table is going to display. This works fine until the deletion and update occurs:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy];
mytable.dataSource = self;
mytable.delegate = self;
}
-
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(!filePathsArray)
{
return 0;
}
if ([filePathsArray count] > 0)
return [filePathsArray count];
else
return 0;
}
-
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
NSString *currentFileName = filePathsArray[indexPath.row];
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:currentFileName];
fileURL = [NSURL fileURLWithPath:filePath];
NSLog(#"urlstring %#",fileURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:filePath error:NULL];
NSLog(#"successfully deleted");
}
}
-
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
NSLog(#"urlstring %#",[filePathsArray objectAtIndex:indexPath.row]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent];
filePathsArray = [[NSArray alloc] initWithArray: [[[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]mutableCopy]];
return cell;
}
-
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
The error i receive is this:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** First throw call stack:
(0x2049012 0x1473e7e 0x1ffeb44 0x3c3a 0x69b8fb 0x69b9cf 0x6841bb 0x694b4b 0x6312dd 0x14876b0 0x2c84fc0 0x2c7933c 0x2c79150 0x2bf70bc 0x2bf8227 0x2bf88e2 0x2011afe 0x2011a3d 0x1fef7c2 0x1feef44 0x1feee1b 0x23dc7e3 0x23dc668 0x5e0ffc 0x2212 0x2145)
libc++abi.dylib: terminate called throwing an exception
(lldb)
I receive this error due to:
NSLog(#"urlstring %#",[filePathsArray objectAtIndex:indexPath.row]);
but once removed it says the problem is with the next line. Can anybody help?
Because of the way you're setting data in filePathsArray, your tableView is rendering one extra cell more than needed.
After you delete a file, update your filePathsArray.
What happens is that in your commitEditingStyle method you delete a file, but you do not update the filePathsArray object to remove the subpath for that file.
The only place where you update filePathsArray is in cellForRowAtIndexPath which is called after numberOfRowsInSection
So basically:
E.g. your filePathsArray contains subpaths for 6 files.
You delete a file in commitEditingStyle but you don't update filePathsArray (which still contains 6 subpaths).
You press the button to reloadData
numberOfRowsInSection is called to get the number of cells to be displayed on your UITableView... This returns [filePathsArray count] which is still 6.
cellForRowAtIndexPath is now called for every row.
Inside the above method, you call filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; so now your filePathsArray has 5 objects because of the deleted file.
But guess what.... Your UITableView has already started rendering 6 rows for the old filePathsArray count.
Now when it renders cell number 6 (indexPath.row = 5), it calls cell.textLabel.text = [filePathsArray[indexPath.row] lastPathComponent]; and the last index of your array is now 4. This causes the crash.
Simply do filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; in your commitEditingStyle and the error should stop.
Also, try not modifying your data source object in cellForRowAtIndexPath. Take the filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil]; call out of there as your array is now being updated in commitEditingStyle.
In tableView:cellForRowAtIndexPath:, remove this code: filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
In tableView:commitEditingStyle:,add this code:
[filePathsArray removeObjectAtIndex:indexPath.row];
This should maintain the integrity of the data you're using to populate the table view (so you don't change the number of items when the table doesn't know about it and you do change it when the table thinks you have).

Resources