Как вставить больше строк в табличное представление одновременно? - PullRequest
0 голосов
/ 21 марта 2012

Я хочу одновременно вставить много строк перед последней строкой в ​​табличное представление, но это добавит строку перед последней строкой и добавит еще две строки в конце. Как это понять? Пожалуйста, помогите мне, спасибо заранее. !

- (void)morePicture:(id)sender{
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i=0; i<3; i++) {
        NSString *s = [[NSString alloc] initWithFormat:@"%d",i];
        [photos addObject:s];
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
        [indexPaths addObject:indexpath];
   }

   [table beginUpdates];
   [table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
   [table endUpdates];

   [table reloadData];
}

введите описание изображения здесь

Ответы [ 3 ]

3 голосов
/ 21 марта 2012
- (void)morePicture:(id)sender {
    // See how many rows there are already:
    NSUInteger rowCount = [table numberOfRowsInSection:0]
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
    for (int i=0; i<3; i++) {
        NSString *s = [[NSString alloc] initWithFormat:@"%d",i];
        [photos addObject:s];
        // The new index path is the original number of rows plus i - 1 to leave the last row where it is. 
        NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i+rowCount - 1 inSection:0];
        [indexPaths addObject:indexpath];
    }

    [table beginUpdates];
    [table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
    [table endUpdates];

    [table reloadData];
}
2 голосов
/ 21 марта 2012

Не уверен, что я понял, что вы имеете в виду, но вам не нужно звонить [table reloadData] после [table endUpdates]

0 голосов
/ 21 марта 2012

Я думаю, что ваша проблема может быть связана с идеей добавления строк перед последней строкой массива. Каждый раз, когда вы добавляете, последняя строка меняется. Если вы можете правильно настроить индексирование, то просто используйте эти индексы в ваших indexPaths, и все это сработает, я думаю:

NSInteger insertPosition = photos.count - 1;  // this index will insert just before the last element
for (int i=0; i<3; i++) {
    NSString *s = [[NSString alloc] initWithFormat:@"%d",insertPosition];  // not i
    [photos insertObject:s atIndex:insertPosition];
    NSIndexPath *indexpath = [NSIndexPath indexPathForRow:insertPosition inSection:0];
    [indexPaths addObject:indexpath];
    insertPosition++;  // advance it, because the end of the table just advanced
}

[table beginUpdates];
[table insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[table endUpdates];
// no need to reload data as @sampage points out
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...