简体   繁体   English

在UITableView中批量插入时,第一个单元格的insertRowAnimation不起作用

[英]insertRowAnimation for the first cell doesn't work when batch inserting in UITableView

Hi Im trying to add an observer for my model to notify UITableview about any changes in the model. 嗨,我正在尝试为模型添加观察者,以将模型中的任何更改通知UITableview。 with this pattern I can decouple the model and view and I can put UITableViewDataSource in another class. 通过这种模式,我可以将模型与视图分离,并将UITableViewDataSource放在另一个类中。

until now everything works well. 到目前为止,一切正常。 but the problem is that when I inserting more than one object in the tableView the first row insert animation doesn't work. 但是问题是当我在tableView中插入多个对象时,第一行插入动画不起作用。

it only happens for UITableViewRowAnimationTop. 它仅适用于UITableViewRowAnimationTop。 and I have tested on the real device. 我已经在真实设备上进行了测试。 but no hope! 但没有希望!

does anyone have any idea how can I fix this? 有谁知道如何解决这个问题?

here is the code: 这是代码:

 - (void)viewDidLoad
{
    [super viewDidLoad];
    Item *item = [Item new];
    item.name = @"Computer";

    Item *item2 = [Item new];
    item2.name = @"lapTop";
    item2.parent = item;

    Item *item3 = [Item new];
    item3.name = @"NoteBook";
    item3.parent = item;

    item.children = [NSSet setWithObjects:item2,item3, nil];

    self.Items = [NSMutableArray arrayWithObject:item];

    [self addObserver:self
           forKeyPath:@"items"
              options:0
              context:NULL];
}

#pragma mark - Table View Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Item *object =(Item *)[self.Items objectAtIndex: indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSLog(@"%@",object.name);

    if (object.parent != NULL) {
        cell.indentationLevel = 1;
        cell.indentationWidth = 25;
    }
    cell.textLabel.text = object.name;
    return  cell;
}

#pragma  mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[self.Items objectAtIndex:indexPath.row];
    if([dic valueForKey:@"children"])
    {
        NSArray *children=[dic valueForKey:@"children"];
        BOOL isTableExpanded=NO;

        for(Item *child in children )
        {
            NSInteger index=[self.Items indexOfObjectIdenticalTo:child];
            isTableExpanded=(index>0 && index!=NSIntegerMax);
            if(isTableExpanded) break;
        }

        if(isTableExpanded)
        {
            [self CollapseRows:children];
        }
        else
        {
            NSUInteger count=indexPath.row+1;
            NSMutableArray *arrCells=[NSMutableArray array];
            for(Item *subItem in children )
            {
                NSLog(@"%@",subItem.name);
                if (subItem.parent != NULL) {
                    [arrCells addObject:[NSIndexPath indexPathForRow:count inSection:0]];
                    [self insertObject:subItem inItemsAtIndex:count++];
                }
            }

            //[self.tableView insertRowsAtIndexPaths:arrCells withRowAnimation:UITableViewRowAnimationTop];

        }
    }
}

-(void)CollapseRows:(NSArray*)ar
{
    for(NSDictionary *dInner in ar )
    {
        NSUInteger indexToRemove=[self.Items indexOfObjectIdenticalTo:dInner];
        NSArray *arInner=[dInner valueForKey:@"children"];
        if(arInner && [arInner count]>0)
        {
            [self CollapseRows:arInner];
        }

        if([self.Items indexOfObjectIdenticalTo:dInner]!=NSNotFound)
        {
            [self removeObjectFromItemsAtIndex:[self.Items indexOfObjectIdenticalTo:dInner]];
            //          [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:
            //                                                        [NSIndexPath indexPathForRow:indexToRemove inSection:0]
            //                                                        ]
            //                                      withRowAnimation:UITableViewRowAnimationTop];
        }
    }
}

#pragma mark KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    NSIndexSet *indices = [change objectForKey:NSKeyValueChangeIndexesKey];
    if (indices == nil)
        return; // Nothing to do


    // Build index paths from index sets
    NSUInteger indexCount = [indices count];
    NSUInteger buffer[indexCount];
    [indices getIndexes:buffer maxCount:indexCount inIndexRange:nil];

    NSMutableArray *indexPathArray = [NSMutableArray array];
    for (int i = 0; i < indexCount; i++) {
        NSUInteger indexPathIndices[2];
        indexPathIndices[0] = 0;
        indexPathIndices[1] = buffer[i];
        NSIndexPath *newPath = [NSIndexPath indexPathWithIndexes:indexPathIndices length:2];
        [indexPathArray addObject:newPath];
    }

    NSNumber *kind = [change objectForKey:NSKeyValueChangeKindKey];
    if ([kind integerValue] == NSKeyValueChangeInsertion)  // Rows were added
        [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];

    else if ([kind integerValue] == NSKeyValueChangeRemoval)  // Rows were removed
        [self.tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];

}

#pragma mark Array Accessors
- (NSUInteger)countOfItems
{
    return [self.Items count];
}

- (id)objectInItemsAtIndex:(NSUInteger)idx
{
    return [self.Items objectAtIndex:idx];
}

- (void)insertObject:(id)anObject inItemsAtIndex:(NSUInteger)idx
{
    //[self willChangeValueForKey:@"menus"];
    [self.Items insertObject:anObject atIndex:idx];
}

- (void)removeObjectFromItemsAtIndex:(NSUInteger)idx
{
    [self.Items removeObjectAtIndex:idx];
}

You can try to add animation directly on the first cell layer in the cellForRowAtIndexPath . 您可以尝试直接在cellForRowAtIndexPath的第一个单元层上添加动画。

animation  = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setFillMode:kCAFillModeBackwards];
[animation setDuration:.2];
[cell.view.layer addAnimation:animation forKey:@"cellAnimation"]

https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CATransition_class/Introduction/Introduction.html https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CATransition_class/Introduction/Introduction.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 插入带有动画的第一行时,UITableView无法正确显示单元格 - UITableView doesn't display the cell correctly when inserting in the first row with animations UITableView的单元格不能用作设置 - UITableView's cell doesn't work as setting uitableview 单元格第一次仍然无法正确删除 - uitableview cell still doesn't delete properly the first time scrollToRowAtIndexPath不适用于第一个单元格 - scrollToRowAtIndexPath doesn't work for first cell 带有UITapGestureRecognizer的UIView上的UITableView(单元格选择不起作用) - UITableView on a UIView with UITapGestureRecognizer (cell selection doesn't work) 当UITableView滚动到不可见的单元格时,UITextField不会获得光标 - UITextField doesn't get cursor when UITableView scrolles to not visible cell UITableView 选中的单元格在滚动时不会保持选中状态 - UITableView selected cell doesn't stay selected when scrolled CLLocationManager不适用于UITableView第一页上的单元格 - CLLocationManager doesn't work for cells on first page of UITableView 我的UITableView的第一项不起作用 - my UITableView's first item doesn't work 在 UITableview 单元格中折叠 UIImageview,当它不返回图像时 - Collapse UIImageview in UITableview cell, when it doesn't return an image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM