Created
December 13, 2013 09:27
-
-
Save booiiing/7941890 to your computer and use it in GitHub Desktop.
Implementation of http://stackoverflow.com/a/17335818/938948
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface IntrinsicTableView : UITableView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "IntrinsicTableView.h" | |
@implementation IntrinsicTableView | |
- (CGSize)intrinsicContentSize { | |
[self layoutIfNeeded]; | |
return CGSizeMake(UIViewNoIntrinsicMetric, self.contentSize.height); | |
} | |
- (void)endUpdates { | |
[super endUpdates]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)reloadData { | |
[super reloadData]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { | |
[super reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { | |
[super reloadSections:sections withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { | |
[super insertRowsAtIndexPaths:indexPaths withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { | |
[super insertSections:sections withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation { | |
[super deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation { | |
[super deleteSections:sections withRowAnimation:animation]; | |
[self invalidateIntrinsicContentSize]; | |
} | |
@end |
Sounds interesting, but there is an easier way to do it, which supports iOS 7 too
Just override -setContentSize method:
- (void)setContentSize:(CGSize)contentSize {
[super setContentSize:contentSize];
[self invalidateIntrinsicContentSize];
}
About this problem,may i have demo with storyboard please ?
Hi patric.schenke, Do you have any Sample Project this función?
Thanks for this! Here is a Swift version that I am using https://gist.github.com/johneris/2263781090f271a7a647
About this problem,may i have demo with storyboard please ? it will be great to support swift also
Thanks, this is Swift 5 version https://gist.github.com/sseno/d0a462c600259272fa6bb96c2a76e619
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found an issue with this code and iOS 7.x
When a table with this custom class is inside a VC that is part of a TabBar VC, the class will seemingly get called recursively (primarily the function 'intrinsicContentSize') until an EXC_BAD_ACCESS 2 error is thrown.
I tested this with multiple VC's that already contained tables with this custom class. They work great when on their own, but as soon they are embedded into a TabBarVC, the error occurs.
This does not happen in iOS 6.x, however.