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

这可能吗?


当前回答

是的,这是可能的。

UITableView有一个委托方法didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:.6
                          delay:0
         usingSpringWithDamping:UIViewAnimationOptionBeginFromCurrentState
          initialSpringVelocity:0
                        options:UIViewAnimationOptionBeginFromCurrentState animations:^{

                            cellindex = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
                            NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
                            [violatedTableView beginUpdates];
                            [violatedTableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
                            [violatedTableView endUpdates];
                        }
                     completion:^(BOOL finished) {
    }];
}

但在你的情况下,如果用户滚动并选择不同的单元格,那么你需要有最后一个选定的单元格来收缩和扩展当前选定的单元格reloadRowsAtIndexPaths:调用highightforrowatindexpath:因此相应地处理。

其他回答

西蒙·李的回答。

// 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()
            }
        }
    }
}

输入- - -

tableView.beginUpdates () tableView.endUpdates () 这些函数不会调用

func tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath) -> UITableViewCell {}

但是,如果你这样做了, 表视图。reloadRows (:(selectedIndexPath !, .none)

它会调用 func tableView(_ tableView: UITableView, cellForRowAt indexPath: indexPath) -> UITableViewCell {} 这个函数。

是的,这是可能的。

UITableView有一个委托方法didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [UIView animateWithDuration:.6
                          delay:0
         usingSpringWithDamping:UIViewAnimationOptionBeginFromCurrentState
          initialSpringVelocity:0
                        options:UIViewAnimationOptionBeginFromCurrentState animations:^{

                            cellindex = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
                            NSArray* indexArray = [NSArray arrayWithObjects:indexPath, nil];
                            [violatedTableView beginUpdates];
                            [violatedTableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
                            [violatedTableView endUpdates];
                        }
                     completion:^(BOOL finished) {
    }];
}

但在你的情况下,如果用户滚动并选择不同的单元格,那么你需要有最后一个选定的单元格来收缩和扩展当前选定的单元格reloadRowsAtIndexPaths:调用highightforrowatindexpath:因此相应地处理。

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

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

[tableView beginUpdates];
[tableView endUpdates];

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

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

只是一个提示,像我这样的人搜索添加“更多细节”自定义单元格。

[tableView beginUpdates];
[tableView endUpdates];

做了一个出色的工作,但不要忘记“裁剪”单元格视图。 从界面构建器中选择单元格->内容视图->从属性检查器中选择“剪辑子视图”