我在我的iPhone应用中使用UITableView,我有一个属于一个组的人的列表。我希望当用户单击一个特定的人(因此选择单元格)时,单元格的高度会增加,以显示用于编辑这个人的属性的几个UI控件。
这可能吗?
我在我的iPhone应用中使用UITableView,我有一个属于一个组的人的列表。我希望当用户单击一个特定的人(因此选择单元格)时,单元格的高度会增加,以显示用于编辑这个人的属性的几个UI控件。
这可能吗?
当前回答
在ios7及以上版本后检查此方法。
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
iOS 8对此进行了改进。我们可以将它设置为表视图本身的属性。
其他回答
我发现了一个非常简单的解决方案,作为一个UITableView的副作用,我正在.....工作
将单元格高度存储在一个变量中,该变量通常通过tableView: highightforrowatindexpath:来报告原始高度,然后当你想要动画化高度变化时,只需改变变量的值并调用这个…
[tableView beginUpdates];
[tableView endUpdates];
你会发现它不会完全重载,但足以让UITableView知道它必须重绘单元格,为单元格....抓取新的高度值你猜怎么着?它使你的改变变得生动。甜的。
我有一个更详细的解释和完整的代码示例在我的博客…动画UITableView单元格高度变化
reloadData不好,因为没有动画…
这是我目前正在尝试的:
NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
它几乎是正确的。几乎。我正在增加单元格的高度,当单元格被替换时,有时在表格视图中会有一点“小问题”,就像在表格视图中保留了一些滚动位置一样,新单元格(表中的第一个单元格)的偏移量过高,而滚动视图会反弹以重新定位它。
BOOL flag;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
flag = !flag;
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES == flag ? 20 : 40;
}
Swift 4及以上
将以下代码添加到tableview的didselect行委托方法中
tableView.beginUpdates()
tableView.setNeedsLayout()
tableView.endUpdates()
是的,这是可能的。
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:因此相应地处理。