6

Context: I am using AsyncDisplayKit to load a table with text and images. The images are downloaded from the Internet and the image sizes are unknown until the image is finished downloading.

What I'm trying to do: After the image is finished downloading, I want to re-size and layout the cell to accommodate for the image and then re-size and layout the table to accommodate for the new cell height.

What I've done so far: I initially draw the ASTableView with just the text and use the ASNetworkingImage delegate method, imageNode:didLoadImage method to get notified when the image is finished downloading.

- (void)imageNode:(ASNetworkImageNode *)imageNode didLoadImage:(UIImage *)image {

    // Scale image downloaded from Internet to fit constrained width
    // Set networkImageNode to scaled image
    UIImage *newimage = [self scaleImage:image toConstrainedWidth:CONSTRAINED_WIDTH]; 
    self.networkImageNode.image = newimage;

    // Gets the parent cell 
    // Recalculate cell size and layout the cell (This works!)
    MyAsyncTableCellNode *parentCell = ASDisplayNodeFindClass(self, [MyAsyncTableCellNode class]);
    [parentCell invalidateCalculatedSize];
    [parentCell measure:CGSizeMake(CONSTRAINED_WIDTH, MAXFLOAT)];
    [parentCell layout];

    // Attempting to re-layout the table to shift the table cells
    // to accommodate for the new cell height (ALL these do NOT work!)
    MyAsyncTableViewController *parentTableViewController = ASDisplayNodeFindClass(self, [MyAsyncTableViewController class]);
    // Attempt #1
    [parentTableViewController.asyncTableView reloadData];

    // Attempt #2
    [parentTableViewController.asyncTableView beginUpdates];
    [parentTableViewController.asyncTableView endUpdates];
}

I'm able to successfully re-calculate the size of the individual cell and re-layout and draw the cell to fit the downloaded image.

Issues: However, next I need to re-layout the table to shift the other cells down or up to fit the new size of the updated cell. THIS IS WHAT I CANNOT GET TO WORK.

Attempt #1 will re-calculate the subviews of the table and re-layout, but that also reloads all the data, which is not what I want. I want to just re-calculate and re-layout the cells, not reload all the data.

Attempt #2: When using UITableView, doing this will call tableView:heightForRowAtIndexPath and re-layout all the table cells, but unfortunately, for ASTableView this throws a not implemented exception.

Question: How do I programmatically make the ASTableView relayout itself and account for the new cell heights?

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50

1 Answers1

0

Your first attempt should be correct because this call again the numberOfSectionsInTableView, numberOfSectionsInTableView, nodeForRowAtIndexPath methods and should not reload the data source unless you specified so (as a regular UITableView do).

Macistador
  • 854
  • 12
  • 23