我有一个带有自动调整单元格大小的 UITableView。表包含 1 个部分和一些行。我有一些行说 30,我想在期间删除行didSelectRowAtIndexPath
.
当我删除 tableview 底部附近的可用行(例如第 28 行)时,第 28 行之前的行会更改其位置,并导致动画出现抖动deleteRowsAtIndexPaths
。有关更多详细信息,请参阅随附的 gif。
用帧删除动画构建的单元格 https://giphy.com/gifs/GvMvf5gXrvpMA
自动调整单元格大小删除动画 https://giphy.com/gifs/K10VNkegCykog
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return noOfRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setCellText:[NSString stringWithFormat:@"Section = %li Row = %li", indexPath.section,indexPath.row]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
noOfRows--;
[tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}
这种不稳定的动画仅发生在自调整大小的单元格上,而不发生在使用框架构建的单元格上。
任何帮助将不胜感激。
EDIT 1:问题在于内容偏移和内容大小。假设表视图中的每个单元格的高度为 50。因此,对于 20 个单元格,内容大小约为 1000。让我们将设备高度视为 200,宽度为 100。因此内容大小将为 100,1000。当我滚动到 tableview 内容底部时,偏移量变为 0,800。现在,当我删除一行时,内容大小将更新为 100、950,并且由于内容大小的更改,内容偏移现在尝试调整为新值。所以这会导致生涩。
动画不稳定是由于细胞的高度估计不正确造成的。所以你需要实施estimatedRowHeight
在您的表视图控制器中并返回保存的高度。您可以将之前加载的单元格高度保存在willDisplayCell
tableView 委托方法为cell.frame.size.height
在一个数组中。
var estimatedHeights : [CGFloat] = []
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
if estimatedHeights.count > indexPath.row
{
return estimatedHeights[indexPath.row]
}
return 40
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if estimatedHeights.count <= indexPath.row
{
estimatedHeights.append(cell.frame.height)
}
else
{
estimatedHeights[indexPath.row] = cell.frame.height
}
}
还将您的删除代码放入[tableView beginUpdates];
and [tableView endUpdates];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)