iOS - dynamically change height of uitableview

Libor Zapletal

I want to create suggestions to UISearchBar so I added UITableView and I want to change height of the UITableView by content.

When I get data I call:

[self.searchTableView reloadData];

and in - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section I am trying to resize tableView:

    CGRect bounds = [tableView bounds];
    NSLog(@"%f",tableView.contentSize.height);
    [tableView setBounds:CGRectMake(bounds.origin.x,
                                    bounds.origin.y,
                                    bounds.size.width,
                                    20 * [self.suggestionData count])];

    /*
    CGRect frame = self.searchTableView.frame;;
    frame.size.height = 20 * [self.suggestionData count];
    self.searchTableView.frame = frame;
     */

(20 is height of 1 row.) I've tried tableView.contentSize.height too but it didn 't works. Maybe there is need to change something in Storyboard or I when I was trying to find solution I change something wrong. I just get height of tableview same as I set in storyboard. Thanks

HepaKKes

Since UITableView extends a UIScrollView I suggest you to use the following approach in order to get a table view whose bounds fit its content size.

In the -init or -awakeFromNib of your special TableView (you can extend the UITableView or create a category) register the observer

[self addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

and then add

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == self && [keyPath isEqualToString:@"contentSize"]) {
        CGRect frame = self.frame;
        CGFloat currentHeight = self.contentSize.height;
        if (fabs(currentHeight - frame.size.height) > FLT_EPSILON) {
            frame.size.height = self.contentSize.height;
            self.frame = frame;
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:nil];
    }
}

Remember to deregister the observer in the -dealloc

- (void)dealloc {
    [self removeObserver:self forKeyPath:@"contentSize"];
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iOS - dynamically change height of uitableview

From Dev

uitableview height does not change dynamically in swift ios?

From Dev

uitableview height does not change dynamically in swift ios?

From Dev

How to change cell height dynamically in UITableView static cell

From Dev

UITableView: How to change cell height dynamically when a button is clicked in it?

From Dev

UITableView: How to change cell height dynamically when a button is clicked in it? Swift

From Dev

Trying to dynamically increase height of UITableView while user adds rows to UITableView in iOS

From Dev

Trying to dynamically increase height of UITableView while user adds rows to UITableView in iOS

From Dev

How to dynamically set the height of a UITableView?

From Dev

how to change UI Button width and height dynamically with respect to image in ios

From Dev

dynamically change actionbar height

From Dev

Change UIView height dynamically

From Dev

Change height of control dynamically

From Dev

UITableView header height will not change in storyboard

From Dev

Change the UITableView's height for HeaderInSection?

From Dev

Change UIView height with respect to UITableview

From Dev

Xamarin.iOS iOS 8 UITableView custom cell height does not change

From Dev

Xamarin.iOS iOS 8 UITableView custom cell height does not change

From Dev

How to dynamically increase height of scrollview based on uitableview

From Dev

How to set Dynamically UITableView height in Swift?

From Dev

IOS UITableView frame change action not firing when I use auto layout constraint to update its frame height

From Dev

Dynamically change TableView Cell height

From Dev

Dynamically change height of slide for jssor

From Dev

Dynamically change cell height programmatically

From Dev

make div change height dynamically

From Dev

Change the height of UISegmentedControl in iOS

From Dev

iOS Change rootViewController Dynamically

From Dev

How to change the column height and width dynamically in PDF file using iOS7?

From Java

How to change height of grouped UITableView header?

Related Related

  1. 1

    iOS - dynamically change height of uitableview

  2. 2

    uitableview height does not change dynamically in swift ios?

  3. 3

    uitableview height does not change dynamically in swift ios?

  4. 4

    How to change cell height dynamically in UITableView static cell

  5. 5

    UITableView: How to change cell height dynamically when a button is clicked in it?

  6. 6

    UITableView: How to change cell height dynamically when a button is clicked in it? Swift

  7. 7

    Trying to dynamically increase height of UITableView while user adds rows to UITableView in iOS

  8. 8

    Trying to dynamically increase height of UITableView while user adds rows to UITableView in iOS

  9. 9

    How to dynamically set the height of a UITableView?

  10. 10

    how to change UI Button width and height dynamically with respect to image in ios

  11. 11

    dynamically change actionbar height

  12. 12

    Change UIView height dynamically

  13. 13

    Change height of control dynamically

  14. 14

    UITableView header height will not change in storyboard

  15. 15

    Change the UITableView's height for HeaderInSection?

  16. 16

    Change UIView height with respect to UITableview

  17. 17

    Xamarin.iOS iOS 8 UITableView custom cell height does not change

  18. 18

    Xamarin.iOS iOS 8 UITableView custom cell height does not change

  19. 19

    How to dynamically increase height of scrollview based on uitableview

  20. 20

    How to set Dynamically UITableView height in Swift?

  21. 21

    IOS UITableView frame change action not firing when I use auto layout constraint to update its frame height

  22. 22

    Dynamically change TableView Cell height

  23. 23

    Dynamically change height of slide for jssor

  24. 24

    Dynamically change cell height programmatically

  25. 25

    make div change height dynamically

  26. 26

    Change the height of UISegmentedControl in iOS

  27. 27

    iOS Change rootViewController Dynamically

  28. 28

    How to change the column height and width dynamically in PDF file using iOS7?

  29. 29

    How to change height of grouped UITableView header?

HotTag

Archive