2013-06-19 3 views
0

Я пытаюсь удалить строку из UITableView в iOS.Ошибка удаления строки UITableView в iOS

В UITableView, я показываю список файлов, который находится в каталоге документа.

Я хочу удалить файл из каталога документов.

Вот код для удаления.

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

     self.fileManager = [NSFileManager defaultManager]; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     [self.fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",cell.textLabel.text]] error:NULL]; 


     [tableView reloadData]; 
    } 
} 

Сообщение об ошибке

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

Однако, когда я пытаюсь удалить, это сбой.

Пожалуйста, помогите мне.

+0

да, я обновил свой вопрос. перепроверить. –

+0

Предоставьте параметр 'NSError *' в 'error:', если есть какая-либо ошибка, и файл не будет удален, вы получите четкую несогласованность. –

ответ

3

Ваш метод numberOfRowsInSection не обновляется должным образом. Вы должны убедиться, что возвращаемое значение изменяется при каждом добавлении или удалении элементов.

Часто лучше всего передавать все данные, с которыми работаете, в NSMutableArray, а затем в numberOfRowsInSection, вы бы использовали что-то вроде return [arrayOfData count];.

+0

есть я использовали возвращение [self.array count]; в моем номереOfRowsInSection. –

+0

Да, но вы, кажется, не удаляете элементы из этого массива в отправленном вами коде. – woz

+0

Итак, как удалить этот элемент из массива? Спасибо вам за ответ. –

1

Он должен отражать возврат к методу, который возвращает количество строк.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//return array count 
} 

удалить элемент из массива. Шаги:
1: индекс элемента поиска из массива
2: удалить метод removeObjectAtIndex.

Смежные вопросы