Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用可变内容调整UITableViewCell的大小?_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 如何使用可变内容调整UITableViewCell的大小?

Ios 如何使用可变内容调整UITableViewCell的大小?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,假设UITableViewCell的子类a具有两种状态: 1.)显示一些收割台(高度:0-50px) 2.)显示一些标题(高度:0-50px)+UITableView(高度:50-150px)(都在子类UITableViewCell内) A的恰好一个单元可以处于状态2,因此高度为200px。所有其他电池都处于状态1,因此高度为50px 子类A由IB实现,默认大小为200px 管理UITableView的控制器保持哪个单元格处于状态2 问题:如何根据状态更改高度 我确实在索引路径中实现了高度,如下

假设UITableViewCell的子类a具有两种状态: 1.)显示一些收割台(高度:0-50px) 2.)显示一些标题(高度:0-50px)+UITableView(高度:50-150px)(都在子类UITableViewCell内)

A的恰好一个单元可以处于状态2,因此高度为200px。所有其他电池都处于状态1,因此高度为50px

子类A由IB实现,默认大小为200px

管理UITableView的控制器保持哪个单元格处于状态2

问题:如何根据状态更改高度

我确实在索引路径中实现了
高度,如下所示:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (self.openTab && indexPath.row == self.openTab.row) {
       return 200.0;
    } else {
       return 50.0;
    }
 }
它不起作用

此外,我还尝试在
单元格中调整高度,以便于rowatinexpath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
   static NSString *CellIdentifier = @"conversationTableViewCell";
   ConversationTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   if (cell == nil) {
      [self.tableView registerNib:[UINib nibWithNibName:@"ConversationTableViewCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
      cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   }

   if (self.openTab && indexPath.row == self.openTab.row) {
      CGRect frame = cell.contentView.bounds;
      frame.size.height = 200.0;
      cell.contentView.bounds = frame;

   } else {
      CGRect frame = cell.contentView.bounds;
      frame.size.height = 50.0;
      cell.contentView.bounds = frame;
   }

   return cell;
}
除了更改contentView边界的高度外,我还将该思想直接应用于框架以及子类A中UITableView的边界/框架。我还尝试了一些其他方法(见注释部分)。什么都不管用


有什么办法可以做到这一点吗?

如果您的条件是正确的,那么您的第一个代码必须工作。也许你忘了

self.tableview.delegate = self
这种方法不适用

- (void)viewDidLoad {
     [super viewDidLoad];    
    self.tableView.delegate=self;
}
以及您的代码:

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (self.openTab && indexPath.row == self.openTab.row) {
       return 200.0;
    } else {
       return 50.0;
    }
}

通过在
layoutSubviews
中手动调整UITableView(已添加到子类A的contentView)的大小来解决此问题,如下所示:

- (void)layoutSubviews
{
  [super layoutSubviews];

  if (self.openTab) {
    CGRect frame = self.tableView.frame;
    frame.size.height = 150.0;
    self.tableView.frame = frame;
  } else {
    CGRect frame = self.tableView.frame;
    frame.size.height = 0;
    self.tableView.frame = frame;
  }
}

编辑:通过编程方式将视图(例如UITableView)添加到UITableViewCell中,而不是使用IB来更改其大小,这一点也很重要。

设置self.openTab属性的代码在哪里?在与所示代码相同的类中,我可以确保条件正常工作……那么,到底是什么不起作用呢?您的
heightForCell
是否为openTab单元格调用?是的,它被调用,并返回正确的高度值,但视图不反映高度,它始终是interface builder中设置的200px,无论返回的高度是50px还是200px…条件工作正常。。。委托也被设置为正确的。。。我甚至可以记录单元格/表格视图的高度,它会像预期的那样返回正确的值,但是视图没有反映高度……我不知道我说了什么。我刚刚举了一个例子,它非常有效。在我的例子中,我做了下一步:1。在情节提要中创建一个tableview,并将委托和数据源设置为2。y放置此方法:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)indepath{if(indepath.row%2==0){return 200.0;}否则{return 50.0;}}
仅此而已。