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

Commit 42f0288

Browse files
author
Adlai Holler
authored
Account for table view cell separator when reporting height (#2883)
1 parent 98a95a6 commit 42f0288

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

AsyncDisplayKit/ASTableView.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,18 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
819819
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
820820
{
821821
ASCellNode *node = [_dataController nodeAtIndexPath:indexPath];
822-
return node.calculatedSize.height;
822+
CGFloat height = node.calculatedSize.height;
823+
824+
/**
825+
* Weirdly enough, Apple expects the return value here to _include_ the height
826+
* of the separator, if there is one! So if our node wants to be 43.5, we need
827+
* to return 44. UITableView will make a cell of height 44 with a content view
828+
* of height 43.5.
829+
*/
830+
if (tableView.separatorStyle != UITableViewCellSeparatorStyleNone) {
831+
height += 1.0 / ASScreenScale();
832+
}
833+
return height;
823834
}
824835

825836
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

AsyncDisplayKitTests/ASTableViewTests.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#import "ASTableView+Undeprecated.h"
2020
#import <JGMethodSwizzler/JGMethodSwizzler.h>
2121
#import "ASXCTExtensions.h"
22+
#import "ASInternalHelpers.h"
2223

2324
#define NumberOfSections 10
2425
#define NumberOfReloadIterations 50
@@ -234,7 +235,7 @@ - (void)testDataSourceImplementsNecessaryMethods
234235
- (void)testConstrainedSizeForRowAtIndexPath
235236
{
236237
// Initial width of the table view is non-zero and all nodes are measured with this size.
237-
// Any subsequence size change must trigger a relayout.
238+
// Any subsequent size change must trigger a relayout.
238239
// Width and height are swapped so that a later size change will simulate a rotation
239240
ASTestTableView *tableView = [[ASTestTableView alloc] __initWithFrame:CGRectMake(0, 0, 100, 400)
240241
style:UITableViewStylePlain];
@@ -249,12 +250,13 @@ - (void)testConstrainedSizeForRowAtIndexPath
249250
[tableView setNeedsLayout];
250251
[tableView layoutIfNeeded];
251252

253+
CGFloat separatorHeight = 1.0 / ASScreenScale();
252254
for (int section = 0; section < NumberOfSections; section++) {
253255
for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
254256
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
255257
CGRect rect = [tableView rectForRowAtIndexPath:indexPath];
256258
XCTAssertEqual(rect.size.width, 100); // specified width should be ignored for table
257-
XCTAssertEqual(rect.size.height, 42);
259+
XCTAssertEqual(rect.size.height, 42 + separatorHeight);
258260
}
259261
}
260262
}
@@ -775,7 +777,7 @@ - (void)testIssue2252
775777
[window layoutIfNeeded];
776778
[node waitUntilAllUpdatesAreCommitted];
777779
XCTAssertEqual(node.view.numberOfSections, NumberOfSections);
778-
ASXCTAssertEqualRects(CGRectMake(0, 32, 375, 43.5), [node rectForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]], @"This text requires very specific geometry. The rect for the first row should match up.");
780+
ASXCTAssertEqualRects(CGRectMake(0, 32, 375, 44), [node rectForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]], @"This text requires very specific geometry. The rect for the first row should match up.");
779781

780782
__unused XCTestExpectation *e = [self expectationWithDescription:@"Did a bunch of rounds of updates."];
781783
NSInteger totalCount = 20;

0 commit comments

Comments
 (0)