У меня есть код, отображающий имя файла в моем uitableview
. однако после удаления файла и обновления я получаю сообщение об ошибке. Вот мой код, чтобы отобразить мое имя файла, мои удалить действия кнопки и ошибка:Objective-C: Как обновить UITableView?
Во-первых, при нажатии кнопки я запускаю этот код, который работает при добавлении файла в таблицу:
-(IBAction)refresh{
[[self mytable] reloadData];
}
Во-вторых, у меня есть этот код, чтобы получить и отобразить значения, которые будет отображаться в таблице. Это прекрасно работает до тех пор, удаление и обновление не происходит:
- (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;
}
Ошибка я получаю это:
*** 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)
Я получаю эту ошибку из-за:
NSLog(@"urlstring %@",[filePathsArray objectAtIndex:indexPath.row]);
, но после удаления он говорит, что проблема заключается в следующей строке. Может ли кто-нибудь помочь?
Мне нравится реализация 'numberOfRowsInSection:'. Вместо 'return filePathsArray.count;' вы делаете две лишние и избыточные проверки (приводя к 3 ненужным путям выполнения), которые не защищают вас от чего-либо, только уменьшают удобочитаемость и путают того, кто пытается прорыть код. –
@ H2CO3 Я «люблю», как вам удается превращать разумные советы в депрессивные комментарии. – herzbube
@herzbube Мне тоже нравится это. Это особая сила. –