UITableViewRowAnimationBottom не работает для последней строки - PullRequest
2 голосов
/ 22 декабря 2010

Я столкнулся с очень похожим вопросом здесь: https://stackoverflow.com/questions/3160796/inserting-row-to-end-of-table-with-uitableviewrowanimationbottom-doesnt-animate,, хотя ответы не были даны.Его код также немного отличался от моего.

У меня есть чрезвычайно простой пример, построенный из шаблона приложения Navigation.

NSMutableArray *items;

- (void)viewDidLoad {
    [super viewDidLoad];
    items = [[NSMutableArray array] retain];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addItem)] autorelease];
}

- (void)addItem{
    [items insertObject:@"new" atIndex:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [items objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [items removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }   
}

Проблема заключается в том, что когда я либо вставляю, либо удаляюсамая последняя строка в таблице, анимация вообще не работает;строка просто появляется или исчезает мгновенно.Это происходит только с UITableViewRowAnimationBottom, но именно эта анимация имеет больше смысла для создания или удаления ячеек таблицы таким способом.Это ошибка в структуре Apple?Или это делается специально?Имеет ли смысл добавить дополнительную ячейку к счетчику, а затем настроить эту ячейку так, чтобы она выглядела так, будто ее вообще нет, просто чтобы обойти это поведение?

Ответы [ 2 ]

5 голосов
/ 05 января 2011

Возможно, с помощью UITableViewRowAnimationBottom новая ячейка добавляется «как есть», без анимации, и все строки под ней анимируются с помощью скольжения.Таким образом, если внизу нет никаких строк, анимация не будет.

0 голосов
/ 11 января 2011

Я использую что-то вроде этого, чтобы оживить внешний вид строки (dis), работает шарм

if (oldRowCount < rowCount)
{
    // insert
    NSMutableArray* indexPaths = [NSMutableArray array];
    while (oldRowCount < rowCount)
    {
        [indexPaths addObject: [NSIndexPath indexPathForRow: oldRowCount inSection: section]];
        oldRowCount = oldRowCount + 1;
    }
    [self.tableView insertRowsAtIndexPaths: indexPaths withRowAnimation: UITableViewRowAnimationTop];
}
else if (oldRowCount > rowCount)
{
    // remove
    NSMutableArray* indexPaths = [NSMutableArray array];
    while (rowCount < oldRowCount)
    {
        [indexPaths addObject: [NSIndexPath indexPathForRow: rowCount inSection: section]];
        rowCount = rowCount + 1;
    }
    [self.tableView deleteRowsAtIndexPaths: indexPaths withRowAnimation: UITableViewRowAnimationTop];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...