iOS7动态更改UITableView高度

时间:2014-02-26 21:15:35

标签: ios objective-c uitableview

请帮我提一下这个问题。我尝试了所有可以在这里找到的选择。

附上我的代码样本(我知道,这很糟糕) 如何动态更改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

2 个答案:

答案 0 :(得分:3)

我明白你要做什么。这是你应该做的:

  1. 为您的UITableView添加高度约束
  2. 将其包装在自定义UIView中
  3. 制作自定义类MyCustomView:UIView
  4. 在IB中将您的包装UIView的课程设置为从第3步到您的班级。
  5. 从IB中的约束连接到您的班级
  6. 在表格视图和班级之间建立联系
  7. 将代码放入新课程:
  8. - (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;
    }
    

答案 1 :(得分:-3)

要更改单元格高度:

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

要更改tableView高度:

tableView.size.height = 100;
相关问题