iOS Recipes (for Mary Smithson) by Matt Drance & Paul Warren

iOS Recipes (for Mary Smithson) by Matt Drance & Paul Warren

Author:Matt Drance & Paul Warren
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
ISBN: 1-934356-74-3
Publisher: The Pragmatic Bookshelf, LLC (271010)


[self repositionShadow:self.contentTopShadow];

self.contentTopShadow.hidden = NO;

} else {

self.topShadow.hidden = YES;

self.contentTopShadow.hidden = YES;

}

The next step, adjusting the bottom shadows, is a little trickier. Because table views receive -layoutSubviews so frequently, we only want to bother adjusting the shadows if they’re showing. How do we know if the bottom shadows are exposed? We need to find out where the bottom of the table content is. “That’s easy,” you might be thinking. “Just get the last cell in the last section and get its frame; if it’s nil, then the bottom clearly isn’t showing.” But what if the last section has no rows? What if we have twenty sections, and the last three sections are empty? We could iterate backwards until we find the row that is definitively last in the table, but doing this inside every call to -layoutSubviews is excessive.

OK, so using the "last cell" may not be reliable. What about the table’s contentSize? If the table runs off the screen, contentSize.y is a valid metric. But it turns out that contentSize is always at least the height of the table itself—even if the actual content is much smaller. So if we have a 460 pixel-high table with a single 44 pixel row, contentSize.y is reported as 460, not 44 as we might expect.

It turns out UIKit already performs this measurement for us when it positions the table footer. If we have a table footer, we can just query its frame to find out the table’s bottom Y coordinate.

What if we don’t have a table footer? Easy: we insert one by overriding the -tableFooterView getter as a lazy initializer. If a footer is already installed, we just use that by messaging the superclass. If a footer is not installed, we insert a hidden, zero height view as the footer. The setter is unchanged, so our view controller can replace the placeholder with a custom footer at any time. This gives us a dependable reference for the table’s proper content height under any circumstances. If we need the placeholder, it’s only created once, and not until the first -layoutSubviews message is received. This gives the calling code a chance to set a custom footer before the placeholder is created unnecessarily.

ShadowedTables/Classes/PRPShadowedTableView.m

- (UIView *)tableFooterView {

UIView *footer = [super tableFooterView];

if (footer == nil) {

if (self.placeholderFooter == nil) {

CGRect footerFrame = self.frame;

footerFrame.size.height = 0;

placeholderFooter = [[UIView alloc] initWithFrame:footerFrame];

}

self.placeholderFooter.hidden = YES;

footer = self.tableFooterView = self.placeholderFooter;

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.