我在我的iPhone应用中使用UITableView,我有一个属于一个组的人的列表。我希望当用户单击一个特定的人(因此选择单元格)时,单元格的高度会增加,以显示用于编辑这个人的属性的几个UI控件。

这可能吗?


当前回答

我发现了一个非常简单的解决方案,作为一个UITableView的副作用,我正在.....工作

将单元格高度存储在一个变量中,该变量通常通过tableView: highightforrowatindexpath:来报告原始高度,然后当你想要动画化高度变化时,只需改变变量的值并调用这个…

[tableView beginUpdates];
[tableView endUpdates];

你会发现它不会完全重载,但足以让UITableView知道它必须重绘单元格,为单元格....抓取新的高度值你猜怎么着?它使你的改变变得生动。甜的。

我有一个更详细的解释和完整的代码示例在我的博客…动画UITableView单元格高度变化

其他回答

我发现了一个非常简单的解决方案,作为一个UITableView的副作用,我正在.....工作

将单元格高度存储在一个变量中,该变量通常通过tableView: highightforrowatindexpath:来报告原始高度,然后当你想要动画化高度变化时,只需改变变量的值并调用这个…

[tableView beginUpdates];
[tableView endUpdates];

你会发现它不会完全重载,但足以让UITableView知道它必须重绘单元格,为单元格....抓取新的高度值你猜怎么着?它使你的改变变得生动。甜的。

我有一个更详细的解释和完整的代码示例在我的博客…动画UITableView单元格高度变化

获取所选行的indexpath。重新加载表。在UITableViewDelegate的highightforrowatindexpath方法中,将所选行的高度设置为不同的高度,对于其他的则返回正常的行高度

在ios7及以上版本后检查此方法。

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

iOS 8对此进行了改进。我们可以将它设置为表视图本身的属性。

西蒙·李的回答。

// MARK: - Variables 
  var isCcBccSelected = false // To toggle Bcc.



    // MARK: UITableViewDelegate
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    // Hide the Bcc Text Field , until CC gets focused in didSelectRowAtIndexPath()
    if self.cellTypes[indexPath.row] == CellType.Bcc {
        if (isCcBccSelected) {
            return 44
        } else {
            return 0
        }
    }

    return 44.0
}

然后在didSelectRowAtIndexPath()中

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.tableView.deselectRowAtIndexPath(indexPath, animated: true)

    // To Get the Focus of CC, so that we can expand Bcc
    if self.cellTypes[indexPath.row] == CellType.Cc {

        if let cell = tableView.cellForRowAtIndexPath(indexPath) as? RecipientTableViewCell {

            if cell.tag == 1 {
                cell.recipientTypeLabel.text = "Cc:"
                cell.recipientTextField.userInteractionEnabled = true
                cell.recipientTextField.becomeFirstResponder()

                isCcBccSelected = true

                tableView.beginUpdates()
                tableView.endUpdates()
            }
        }
    }
}

我刚刚用一个小技巧解决了这个问题:

static int s_CellHeight = 30;
static int s_CellHeightEditing = 60;

- (void)onTimer {
    cellHeight++;
    [tableView reloadData];
    if (cellHeight < s_CellHeightEditing)
        heightAnimationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(onTimer) userInfo:nil repeats:NO] retain];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (isInEdit) {
            return cellHeight;
        }
        cellHeight = s_CellHeight;
        return s_CellHeight;
}

当我需要扩展单元格高度时,我设置isInEdit = YES并调用方法[self onTimer],它会动画单元格的增长,直到它达到s_cellhightediting值:-)