【问题标题】:Change UITableView height dynamically iOS7 [duplicate]动态更改UITableView高度iOS7 [重复]
【发布时间】:2014-03-29 23:48:51
【问题描述】:

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

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

【问题讨论】:

  • 你想动态改变tableView的高度还是单元格的高度。
  • 我想改变tableView的常用尺寸。我确实已经改变了单元格的高度。
  • 我认为这个答案应该更符合您的要求stackoverflow.com/a/18784825/457998
  • @chintanadatiya 哦哦!你这个摇滚人!谢谢!现在可以了!

标签: ios objective-c uitableview


【解决方案1】:

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

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

【讨论】:

【解决方案2】:

更改单元格高度:

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

改变tableView的高度:

tableView.size.height = 100;

【讨论】:

  • 我怎样才能把这个表格视图中所有单元格的高度相加?
  • cellheight * 单元格数?
  • 不,我的单元格有不同的高度
  • 你可以循环遍历uitableview中的每个单元格并将它们相加。但这听起来像一个糟糕的用户界面。只是为了确认您确定要保持桌子的高度动态吗?如果有 100 个元素怎么办?此外 TableView 包含滚动视图,如果内容超出指定高度,它将自动滚动。也看看这个答案stackoverflow.com/questions/14223931/…
  • 问题是关于 tableview 而不是 Cell ,请相应地编辑答案
猜你喜欢
  • 2012-12-22
  • 2013-06-15
  • 2014-09-30
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
相关资源
最近更新 更多