在iOS7中动态更改UITableView高度

在iOS7中动态更改UITableView高度,ios,objective-c,uitableview,Ios,Objective C,Uitableview,请帮我问这个问题。我尝试了我能在这里找到的所有选择 我的代码示例附在附件中(我知道,这很糟糕)。 如何动态更改UITableView高度 - (void)viewDidLoad { [super viewDidLoad]; [self vozvratmassiva]; } - (NSMutableArray *) vozvratmassiva { ... return array; } -(NSInteger)numberOfSectionInTableView:

请帮我问这个问题。我尝试了我能在这里找到的所有选择

我的代码示例附在附件中(我知道,这很糟糕)。
如何动态更改
UITableView
高度

- (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

要更改单元格高度,请执行以下操作:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
return commonsize;
}
要更改tableView高度,请执行以下操作:

tableView.size.height = 100;

我明白你想做什么。以下是您应该做的:

  • 向UITableView添加高度约束
  • 将其包装在自定义UIView中
  • 创建自定义类MyCustomView:UIView
  • 从步骤3开始,在IB中将wraper UIView的类设置为类
  • 将IB中的约束连接到您的类
  • 在表视图和类之间建立连接
  • 将代码放入新类:

  • 是否要动态更改tableView的高度或单元格的高度。我要更改tableView的常用大小。我已经改变了牢房的高度。我认为这个答案应该更符合你的要求@chintanadatiya ooooooh!你这个摇滚人!非常感谢。现在它工作了!如何计算此tableview中所有单元格的高度?单元格高度*单元格数?不,我的单元格有不同的高度。您可以在uitableview中循环每个单元格并将其相加。但这听起来像是一个糟糕的用户界面。只是确认一下,您确定要保持桌子的高度动态吗?如果有100个元素呢?此外,TableView还包括滚动视图,如果内容超出指定高度,滚动视图将自动滚动。再看看这个答案,问题是关于tableview而不是Cell的,请根据需要编辑答案。你不认为你应该将这个答案添加为评论吗?Kirill的答案可能对这个链接中的某人有所帮助->
    - (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;
    }