iphone - UITableViewRowAnimationBottom 不适用于最后一行

标签 iphone objective-c ios uitableview

我在这里遇到了一个非常类似的问题:https://stackoverflow.com/questions/3160796/inserting-row-to-end-of-table-with-uitableviewrowanimationbottom-doesnt-animate ,虽然没有给出答案。他的代码也和我的有点不同。

我有一个非常简单的示例,是根据导航应用程序模板构建的。

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框架中的错误吗?或者它是故意这样做的吗?在计数中添加一个额外的单元格,然后设置该单元格,使其看起来根本不存在,只是为了解决此问题,是否有意义?

最佳答案

也许,使用 UITableViewRowAnimationBottom 新单元格会“按原样”添加,没有动画,并且其下方的所有行都通过滑动进行动画处理。因此,如果下面没有任何行,则不会有动画。

关于iphone - UITableViewRowAnimationBottom 不适用于最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4512922/

相关文章:

iphone - iPhone 推送通知声音有限制吗?

iphone - 如何在 iPhone SDK 中集成 Google Image Search API?

ios - iOS 13 中的 UITabBar 透明标签错误

iphone - AudioQueueGetProperty(queueObject, kAudioQueueProperty_IsRunning, &status, &statusSize) 为状态返回垃圾

ios - 在 IOS 6 上运行的应用程序在 IOS 5 中崩溃

ios - 使用RestKit发出POST请求

javascript - 缩放时如何在 IOS 浏览器上定位固定位置元素?

iphone - 如何在iPhone上更快地插入像素?

objective-c - 在 objective-c 中的两个 View Controller 之间共享对象

ios - 如何按 UITableViewCell 的字母顺序对对象进行排序?