2015-03-04 2 views
0

У меня есть пользовательский UITableView Я пытаюсь добавить строку в tableView. Вот мой код:Добавление строки в таблицуView не работает

- (IBAction)addRow:(id)sender 
{ 
    NSInteger indexPath = [self.rowArray count] - 1; 

    // I'm adding an object to an array of all the cell ids I have. 
    [self.rowArray insertObject:@"rowSix" atIndex:indexPath]; // rowSix is a cell ID 

    [self.myTableView beginUpdates]; 
    [self.myTableView endUpdates]; 

} 

Это ошибка, что он выводит:

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 (8) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 

Update

В cellForRowAtIndexPath:

NSString *cellID = [self.rowArray objectAtIndex:[indexPath row]]; 
customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; 
+0

Если у вас есть массив сотовых идентификаторов, вы, вероятно, делать что-то не так. У вас обычно есть только один идентификатор для каждого типа ячейки. У вас есть несколько типов ячеек (разные подклассы)? – rdelmar

+0

использовать [self.myTableView reloadData]; вместо '[self.myTableView beginUpdates]; [self.myTableView endUpdates]; ' –

+0

@rdelmar У меня разные прототипы ячеек. –

ответ

1

Попробуйте одно-

//Update data source with the object that you need to add 
[tableDataSource addObject:newObject]; 

NSInteger row = //specify a row where you need to add new row 
NSInteger section = //specify the section where the new row to be added, 

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight]; 
[self.tableView endUpdates]; 
+0

См. Обновленный вопрос. Это ничего не добавляет –

1

попробовать это -

- (IBAction)addRow:(id)sender 
    { 
     [self.rowArray addObject:@"New Item"]; 
     NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.rowArray count] inSection:0]; 
     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
+0

Я пробовал это, он работает, но он не анимирует его –

+0

теперь вы использовали insertRowsAtIndexPaths: withAnimation Method между '[self.myTableView beginUpdates]; [self.myTableView endUpdates]; ' –

+0

Когда я это делаю, он уменьшает количество других ячеек. Не знаете, почему –

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