AsyncDisplayKit icon indicating copy to clipboard operation
AsyncDisplayKit copied to clipboard

When is the best time to auto-scroll ASTableView when CellNode's height has changed?

Open Gocy015 opened this issue 10 years ago • 8 comments

Hi , I have a custom ASCellNode , which has a ASNetworkImageNode in it , and this networkImageNode will only be displayed when a button inside the cellNode is tapped.

My problem is , when the image download is complete ,and i call [self setNeedsLayout] in the cellNode to layout the networkImageNode , and then i notify the ASTableView to auto-scroll by overriding the cellNode's -(void)layoutDidFinish , i'm doing this because after the image is downloaded and displayed , the height of the current cellNode will change and the tableView may need to scroll in order to see the whole cell .But it seems that when i try to scroll the last few indexes of the tableView , it doesn't work . After a few tests , It seems that the tableView's contentSize isn't correct , it's hard to describe , may be if you can see my code and find out where i did wrong ?

Thank you very much !

Gocy015 avatar Sep 22 '16 09:09 Gocy015

@Gocy015 - what version of AsyncDisplayKit are you using? If you update to 2.0, and move everything to the table node, this may resolve itself? @Adlai-Holler

hannahmbanana avatar Nov 17 '16 09:11 hannahmbanana

I've update my project to the latest version of ASDK (2.0 beta-1) ,and the auto - scrolling is still incorrect. My call stack looks like this:


#0 -[ViewController cellNode:didExpand:] 
#1 -[InfoCellNode layoutDidFinish]
#2:: -[ASDisplayNode __layout]()
#3:: -[_ASDisplayLayer layoutSublayers]() 

and inside [cellNode: didExpand:] , i try to scroll the tableNode:


-(void)cellNode:(InfoCellNode *)cellNode didExpand:(BOOL)expand{
    if (expand) {
        NSIndexPath *idx = [_tableNode indexPathForNode:cellNode];
        CGRect rect = [_tableNode rectForRowAtIndexPath:idx];
        if (!CGRectContainsRect(self.tableNode.bounds, rect)) {
            NSLog(@"Scroll %lu",idx.row);
            [self.tableNode invalidateCalculatedLayout];
            [self.tableNode scrollToRowAtIndexPath:idx atScrollPosition:UITableViewScrollPositionBottom animated:YES];
        }
    }
    
}

It's not working when the last few indexPaths expand.

Gocy015 avatar Nov 17 '16 10:11 Gocy015

@Gocy015 Calling scrollToRowAtIndexPath:atScrollPosition:animated right after the invalidateCalculatedLayout will have no effect because the cells will probably have the same layout. Try wrapping the scrollToRowAtIndexPath: call inside a dispatch_async(dispatch_get_main_queue()

// workaround
dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableNode scrollToRowAtIndexPath:idx atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});

gkpetrov89 avatar Dec 05 '16 14:12 gkpetrov89

@EviluS Thx ! This solved my problem ,but i'm still a little confused . I dug into the source code to see what [ASDisplayNode invalidateCalculatedLayout] does , and it simply marked the current layout dirty.

I wonder why wrapping scrollToRoAtIndexPath: inside an async method GUARANTEES that the calculation of the new layout will happen before the scrolling method ,since ASDK do background calculations only when the current runloop is "waiting" , what if the current loop is so busy ,that ASDK cannot finish all the calculations between "before waiting" and "after waiting", and in the next loop, when the scrolling method is called , will the scrolling still behave correctly ?

Gocy015 avatar Dec 06 '16 01:12 Gocy015

@Gocy015 This is definitely not a 100% guarantee. The cleanest way is to know when all the layout is done and then call the scrollToRoAtIndexPath: method.

gkpetrov89 avatar Dec 06 '16 09:12 gkpetrov89

@EviluS , So the best way is to subclass ASTableNode ,override layoutDidFinish , and then try to scroll the table node ?

Gocy015 avatar Dec 06 '16 09:12 Gocy015

@Gocy015 You can try it :)

gkpetrov89 avatar Dec 06 '16 09:12 gkpetrov89

@EviluS , Ok, i'll give it a shot , thx for the help 👍 :)

Gocy015 avatar Dec 06 '16 09:12 Gocy015