RATreeView icon indicating copy to clipboard operation
RATreeView copied to clipboard

Inserting an item where sibling items are expanded

Open gshively opened this issue 9 years ago • 0 comments

When insertItemsAtIndexes:inParent:withAnimation, of which my use case is appending as the last child item, it looks like any previous expand sibling's child items are also being included to determine where the index is located.

If I have something like the following:


#pragma mark - UIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupRATreeView];
    self.count = 1;
    [self.treeView reloadData];
    id item = [self treeView:self.treeView child:0 ofItem:nil];
    [self.treeView expandRowForItem:item];

    dispatch_async(dispatch_get_main_queue(), ^{
        self.count = 2;
        [self.treeView insertItemsAtIndexes:[NSIndexSet indexSetWithIndex:1]
                                   inParent:nil
                              withAnimation:RATreeViewRowAnimationAutomatic];

    });
}

#pragma mark - RATreeView DataSource

- (NSInteger)treeView:(RATreeView * __nonnull)treeView numberOfChildrenOfItem:(nullable id)item
{
    if (item == nil) {
        return self.count;
    }
    else if ([item isKindOfClass:[NSNumber class]]) {
        return 2;
    }
    else {
        return 0;
    }
}

- (id __nonnull)treeView:(RATreeView * __nonnull)treeView child:(NSInteger)index ofItem:(nullable id)item
{
    static NSString *alphabet = @"abcdef";

    if (item == nil) {
        return [NSNumber numberWithInteger:index + 1];
    }
    else {
        return [NSString stringWithFormat:@"%c", [alphabet characterAtIndex:index]];
    }
}

- (UITableViewCell * __nonnull)treeView:(RATreeView * __nonnull)treeView cellForItem:(nullable id)item
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (item == nil) {
        cell.textLabel.text = @"Nil";
    }
    else if ([item isKindOfClass:[NSNumber class]]) {
        cell.textLabel.text = [NSString stringWithFormat:@"%@.", item];
    }
    else if ([item isKindOfClass:[NSString class]]) {
        cell.textLabel.text = [NSString stringWithFormat:@"\t%@.", item];
    }

    return cell;
}

I'd expect to have:

1.
    a.
    b.
2.

but instead I get:

1.
    a.
    a.
    b.

If I walk thru all of the sibling items and collapse prior to inserting, and then re-expanding after the insert - it seems to work with some extra animations. I am having to run this on an older XCode, so I'm not sure if the issue is with the older SDK I'm running.

gshively avatar Jan 22 '16 23:01 gshively