0

我有一个UITableView,单元格有不同的随机高度。每个单元格都包含一个按钮,当按下按钮时,我想获得那个特定的单元格高度。我该怎么做呢?

4

4 回答 4

2
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

   //Here calculate your height
   return height;

}

现在无论您想在哪里找到单元格高度

首先找到indexpath

    NSIndexPath *indexPath = [tableView indexPathForCell:cell]

然后找到该索引路径上的单元格的高度,如下所示

   CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
于 2014-02-06T11:50:46.780 回答
0

你可以试试这个。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Calculate a height based on a cell
    if (!self.cutomCell)
    {
        self.cutomCell = [self.myTableView dequeueReusableCellWithIdentifier:@"CustomCell2"];
    }

    // Configure the cell
    self.cutomCell.introductionLabel.text   = self.infoArray[indexPath.row];


    // Layout the cell
    [self.cutomCell layoutIfNeeded];


    // Get the height for the cell
    CGFloat height = [self.cutomCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    return height;
}
于 2014-05-22T10:19:11.990 回答
0

我认为您正在追求这样的东西;

-(IBAction)buttonPressed:(id)sender {

    UIButton *myButton = (UIButton *)sender;
    NSLog(@"%0.2f", myButton.superview.frame.size.height);

}

来自文档: “视图可以嵌入其他视图并创建复杂的视觉层次结构。这在嵌入的视图(称为子视图)和执行嵌入的父视图(称为父视图)之间创建了父子关系。”

此外,您需要将您的按钮分配给上述方法。这可以在界面生成器中完成,或者可以在cellForRowAtIndexPath看起来类似于此的方式中完成:

UIButton *heightButton = (UIButton *)[cell viewWithTag:1];
[heightButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
于 2013-02-04T14:36:06.437 回答
0

heightForRowAtIndexPath不用于获取特定单元格的高度。虽然这是 UITableView 的 DataSource 方法,用于设置UITableViewCell加载时的高度:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0;  // Set the cell height
}
于 2013-02-03T07:14:45.510 回答