Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C: How to refresh a UITableView?
    primarykey
    data
    text
    <p>I have some code that displays file name in my <code>uitableview</code>. 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:</p> <p><strong>Firstly when a button is pressed i run this code which works when adding a file to the table:</strong></p> <pre><code>-(IBAction)refresh{ [[self mytable] reloadData]; } </code></pre> <p><strong>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:</strong></p> <pre><code>- (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; } </code></pre> <p>-</p> <pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(!filePathsArray) { return 0; } if ([filePathsArray count] &gt; 0) return [filePathsArray count]; else return 0; } </code></pre> <p>-</p> <pre><code>- (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"); } } </code></pre> <p>-</p> <pre><code>-(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; } </code></pre> <p>-</p> <pre><code>-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } </code></pre> <p><strong>The error i receive is this:</strong></p> <pre><code> *** 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) </code></pre> <p>I receive this error due to:</p> <pre><code>NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]); </code></pre> <p>but once removed it says the problem is with the next line. Can anybody help?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload