Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UITableView height dynamically iOS7 [duplicate]

Please help me to ask this question. I tried all the choices that I could find here.

The sample of my code is attached (I know, it's pretty bad).
How can I change UITableView height dynamically?

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [self vozvratmassiva];
}

- (NSMutableArray *) vozvratmassiva
{
 ...
    return array;
}

-(NSInteger)numberOfSectionInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *a = [self vozvratmassiva];
    return a.count;
}

-(UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     ....
    return commonsize;
}

@end
like image 597
serg1991 Avatar asked Feb 26 '14 21:02

serg1991


1 Answers

I understand what you're trying to do. Here is what you should do:

  1. Add height constraint to your UITableView
  2. Wrap it in custom UIView
  3. Make a custom class MyCustomView:UIView
  4. Set class in IB for your wraper UIView to your class from step 3.
  5. Make connection from constraint in IB to your class
  6. Make a connection between table view and your class
  7. Put code into your new class:
- (void) layoutSubviews {
  // set height 0, will calculate it later
  self.constraint.constant = 0;

  // force a table to redraw
  [self.tableView setNeedsLayout];
  [self.tableView layoutIfNeeded];

  // now table has real height
  self.constraint.constant = self.tableView.contentSize.height;
}
like image 118
Nikita Took Avatar answered Nov 07 '22 23:11

Nikita Took