Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
[ASTableView] Add constrainedSizeForRowAtIndexPath: to control row he…
Browse files Browse the repository at this point in the history
…ights from delegate (#1769)

* [ASTableView] constrainedSizeForRowAtIndexPath

* Quick fix to header file

* Switch to Delegate from DataSource.

* Update testing variables to reflect switch to delegate
  • Loading branch information
Hannah Troisi authored and appleguy committed Jun 27, 2016
1 parent ae87717 commit db04f4b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
12 changes: 12 additions & 0 deletions AsyncDisplayKit/ASTableView.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,18 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (BOOL)shouldBatchFetchForTableView:(ASTableView *)tableView;

/**
* Provides the constrained size range for measuring the row at the index path.
* Note: the widths in the returned size range are ignored!
*
* @param tableView The sender.
*
* @param indexPath The index path of the node.
*
* @returns A constrained size range for layout the node at this index path.
*/
- (ASSizeRange)tableView:(ASTableView *)tableView constrainedSizeForRowAtIndexPath:(NSIndexPath *)indexPath;

/**
* Informs the delegate that the table view did remove the node which was previously
* at the given index path from the view hierarchy.
Expand Down
17 changes: 15 additions & 2 deletions AsyncDisplayKit/ASTableView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#import <CoreFoundation/CoreFoundation.h>

static const ASSizeRange kInvalidSizeRange = {CGSizeZero, CGSizeZero};
static NSString * const kCellReuseIdentifier = @"_ASTableViewCell";

//#define LOG(...) NSLog(__VA_ARGS__)
Expand Down Expand Up @@ -125,6 +126,7 @@ @interface ASTableView () <ASRangeControllerDataSource, ASRangeControllerDelegat
unsigned int asyncDelegateScrollViewWillEndDraggingWithVelocityTargetContentOffset:1;
unsigned int asyncDelegateTableViewWillBeginBatchFetchWithContext:1;
unsigned int asyncDelegateShouldBatchFetchForTableView:1;
unsigned int asyncDelegateTableViewConstrainedSizeForRowAtIndexPath:1;
} _asyncDelegateFlags;

struct {
Expand Down Expand Up @@ -316,6 +318,8 @@ - (void)setAsyncDelegate:(id<ASTableViewDelegate>)asyncDelegate
_asyncDelegateFlags.asyncDelegateShouldBatchFetchForTableView = [_asyncDelegate respondsToSelector:@selector(shouldBatchFetchForTableView:)];
_asyncDelegateFlags.asyncDelegateScrollViewWillBeginDragging = [_asyncDelegate respondsToSelector:@selector(scrollViewWillBeginDragging:)];
_asyncDelegateFlags.asyncDelegateScrollViewDidEndDragging = [_asyncDelegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)];
_asyncDelegateFlags.asyncDelegateTableViewConstrainedSizeForRowAtIndexPath = [_asyncDelegate respondsToSelector:@selector(tableView:constrainedSizeForRowAtIndexPath:)];

}

super.delegate = (id<UITableViewDelegate>)_proxyDelegate;
Expand Down Expand Up @@ -1069,8 +1073,17 @@ - (ASCellNodeBlock)dataController:(ASDataController *)dataController nodeBlockAt

- (ASSizeRange)dataController:(ASDataController *)dataController constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{
return ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0),
CGSizeMake(_nodesConstrainedWidth, FLT_MAX));
ASSizeRange constrainedSize = kInvalidSizeRange;
if (_asyncDelegateFlags.asyncDelegateTableViewConstrainedSizeForRowAtIndexPath) {
ASSizeRange delegateConstrainedSize = [_asyncDelegate tableView:self constrainedSizeForRowAtIndexPath:indexPath];
// ignore widths in the returned size range (for TableView)
constrainedSize = ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, delegateConstrainedSize.min.height),
CGSizeMake(_nodesConstrainedWidth, delegateConstrainedSize.max.height));
} else {
constrainedSize = ASSizeRangeMake(CGSizeMake(_nodesConstrainedWidth, 0),
CGSizeMake(_nodesConstrainedWidth, FLT_MAX));
}
return constrainedSize;
}

- (NSUInteger)dataController:(ASDataController *)dataController rowsInSection:(NSUInteger)section
Expand Down
41 changes: 40 additions & 1 deletion AsyncDisplayKitTests/ASTableViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ - (ASCellNode *)tableView:(ASTableView *)tableView nodeForRowAtIndexPath:(NSInde
return textCellNode;
}


- (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ^{
Expand All @@ -140,12 +139,52 @@ - (ASCellNodeBlock)tableView:(ASTableView *)tableView nodeBlockForRowAtIndexPath

@end

@interface ASTableViewFilledDelegate : NSObject <ASTableViewDelegate>
@end

@implementation ASTableViewFilledDelegate

- (ASSizeRange)tableView:(ASTableView *)tableView constrainedSizeForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ASSizeRangeMakeExactSize(CGSizeMake(10, 42));
}

@end

@interface ASTableViewTests : XCTestCase
@property (atomic, retain) ASTableView *testTableView;
@end

@implementation ASTableViewTests

- (void)testConstrainedSizeForRowAtIndexPath
{
// Initial width of the table view is non-zero and all nodes are measured with this size.
// Any subsequence size change must trigger a relayout.
// Width and height are swapped so that a later size change will simulate a rotation
ASTestTableView *tableView = [[ASTestTableView alloc] __initWithFrame:CGRectMake(0, 0, 100, 400)
style:UITableViewStylePlain];

ASTableViewFilledDelegate *delegate = [ASTableViewFilledDelegate new];
ASTableViewFilledDataSource *dataSource = [ASTableViewFilledDataSource new];

tableView.asyncDelegate = delegate;
tableView.asyncDataSource = dataSource;

[tableView reloadDataImmediately];
[tableView setNeedsLayout];
[tableView layoutIfNeeded];

for (int section = 0; section < NumberOfSections; section++) {
for (int row = 0; row < NumberOfRowsPerSection; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
XCTAssertEqual(rect.size.width, 100); // specified width should be ignored for table
XCTAssertEqual(rect.size.height, 42);
}
}
}

// TODO: Convert this to ARC.
- (void)DISABLED_testTableViewDoesNotRetainItselfAndDelegate
{
Expand Down

0 comments on commit db04f4b

Please sign in to comment.